본문 바로가기
Java

[Java] == 으로 문자열 비교가 안되는 이유

by palbokdev 2022. 11. 2.

== 는 주소가 동일한지 비교 (같은 객체인지)

.equals()는 값이 동일한지 비교

 

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false

 

ref : https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java