카테고리 없음

어렵다

열하나요 2023. 9. 20. 18:42

NULL

1. 언제 null / "" / 값이 들어가는지

 


- 특히 객체를 null로 선언하냐, new 인스턴스로 선언하냐에 따라 다른거 같은데..
- != null / isEmpty 차이..

1) ArrayList list =  null;이면 if(list.isEmpty)는 nullPointException 발생하기 때문에

if(list != null) 로 조건처리한다. 

2) ArrayList list = new ArrayList();는 null이 발생하지 않기 때문에

if(list.isEmpty)로 조건을 걸어준다.

 

String str = ""는 빈문자열이 들어가있는 것이다. 2)번과 동일한 경우이다.(공백도 값으로 처리)

즉 str == null에는 false이고, str은 isEmpty가 true가 된다.

ArrayList list = new ArrayList();
if(list == null) {
    System.out.println("안녕");  // 미출력
}
if(list.isEmpty()) {
    System.out.println("잘가"); // 출력
}

ArrayList list2 = null;
if(list2 == null) {
    System.out.println("안녕2"); // 출력
}
if(list2.isEmpty()) {
    System.out.println("잘가2"); // NullPointerException
}

String str = "";
if(str == null) {
    System.out.println("스트링널"); // 미출력
}
if(str.isEmpty()) {
    System.out.println("스트링엠티"); // 출력
}

String str2 = null;
if(str2 == null) {
    System.out.println("스트링널2"); // 출력
}
if(str2.isEmpty()) {
    System.out.println("스트링엠티2"); // NullPointerException
}

 

 

 

 

 


2. 경로 찾기 어떨땐 / 가 WebContent이고 어떤땐 classes이고..
3. 저장된 값이 언제까지 살아있나 어떻게 가져오나
4. 지금 어떤 값이 필요한가
5. null값 예외처리 해주는 때는 언제인가
6. (뭐가 value에 값이 들어있고, text/html에 들어있는지) 기본값이 무엇인지 ""인지, select처럼 null값인지

 

7. 

String => Integer : Integer.parseInt("값");

int => String : String.valueOf(값);