File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ toString()과valueOf()에 대해 찾아봤다.
2+ 두 개다 string을 반환을 해준다.
3+
4+ 두 개의 차이점은 NPE(null point exception)발생기는 것이다.
5+ toString에 경우 object가 null이면 NPE이를 발생시키지만
6+ valueOf는 NP가 발생하지 않는다.
7+
8+ 코드를 사용할 떄 NPE 방지를 위해 valueOf를 쓰는게 좋다.
9+
10+ ``` java
11+ destItemMap. get(" LOWER_VAL" ) 이 null 일 경우
12+ String lowerCoatingVal1 = String . valueOf(destItemMap. get(" LOWER_VAL" ));
13+ String lowerCoatingVal2 = destItemMap. get(" LOWER_VAL" ). toString();
14+
15+ lowerCoatingVal1 = " null"
16+ lowerCoatingVal2 = NullPointerException 발생
17+
18+ String . valueOf()의 null 체크
19+ String lowerCoatingVal1 = String . valueOf(destItemMap. get(" LOWER_VAL" ));
20+ if (" null" . equals(lowerCoatingVal1)) {
21+ // To do Somting....
22+ }
23+
24+ // equals함수를 사용할때 왼쪽에 있는 것을 기준으로 비교하기 때문에 변수보다는 문자열을 왼쪽에 두는 것을 추천한다.
25+ // 즉 strTestVal이 null인 경우 ret = "1"인 if문은 NPE를 발생시킨다.
26+ String strTestVal = null ;
27+ String ret = " " ;
28+
29+ /* Exception 발생 */
30+ if ( ! (strTestVal .equals(" " )) ) ret = " 1" ;
31+
32+ /* 정상 */
33+ if ( ! (" " . equals(strTestVal)) ) ret = " 2" ;
34+ ```
You can’t perform that action at this time.
0 commit comments