반응형
오늘 자바에서 한가지 황당한 삽질을 했네요. 문자열 비교를 하는데...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if("Same" == "Same") { | |
return true; | |
} else { | |
return false; | |
} |
이러한 소스가 있었습니다. 전 당연히 true가 나올줄 알았습니다.
그러나 몇차례 삽질을 한 결과 답은 false였습니다.
검색해본 결과 ==은 주소값을 비교, .equals나 .equalsIgnoreCase는 내용물을 비교하는 거라 그렇다고 하네요.
아래와 같이 수정하니 작동하였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if("Same".equalsIgnoreCase("Same")) { | |
return true; | |
} else { | |
return false; | |
} |
Tl;dr
equals이나 equalsIgnoreCase로 비교해야만 합니다.
반응형