'비어있음'이 null
이라는 값으로 표현
null
은 참조형 변수(Reference Type)만 가질 수 있는 값!
Person p1 = null;
System.out.println(p1); // null
p1.getName(); // Exception in thread "main" java.lang.NullPointerException
null 확인
Person[] people = new Person[5];
people[0] = new Person("김신의", 28);
people[2] = new Person("문종모", 26);
people[3] = new Person("서혜린", 21);
for (int i = 0; i < people.length; i++) {
Person p = people[i];
if (p != null) {
System.out.println(p.getName());
} else {
System.out.println(i + "번 자리는 비었습니다.");
}
}
String nullString = null;
String emptyString = "";
System.out.println(null == nullString); // true
System.out.println(nullString.equals("")); // NullPointerException 발생!!
System.out.println(null == emptyString); // false
System.out.println(emptyString.equals("")); // true, 에러 발생 안함.