Programming/Java
[JAVA] LIST 객체 null 체크
bisi
2019. 4. 16. 12:49
java string 객체 null 체크를 위하여 몇가지 테스트를 해봅니다.
null, isEmpty 비교
< 실행 코드 >
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List aa = new ArrayList<>();
System.out.println( aa == null);
System.out.println( aa.isEmpty());
}
}
< 결과 >
false
true
- aa는 이미 new ArrayList로 메모리에 할당되어 있는 상태여서 NULL과 동일하지 않다.
- aa 안에 할당되어 있는 값이 없기 때문에 isEmpry 체크시 true를 출력한다.
결론 : LIST 안에 할당되어 있는 값 존재 여부는 null이 아닌 isEmpty()를 사용하여 체크해준다.