0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

空文字とnullの違い

Posted at

null

null は「箱が空っぽで存在もしないイメージ」
変数 = 箱

null = 箱の中に何も入っていない、しかも箱自体がまだ置かれていない感じ

String str = null;

イメージ:

str  [ なし ]

箱(str)は存在しているけど、中身(オブジェクト)がない
だから「str.length()」とか使うと怒られる(NullPointerException)

空文字

空文字は「中身が空の箱」
変数 = 箱
空文字 = 箱の中に「空の紙」が入っているイメージ

String str = "";

イメージ:

str  [ "" ]   長さゼロの文字が入っている

簡単な覚え方

null = そもそも存在していない
"" = 存在はしているけど中身が空

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?