0
String compare question
Class A { String str1="test"; String str2="TEST".toLowerCase(); If(str1==str2) System.out.println("Compare"); else System.out.println("Not Compare"); } The result is Not Compare though str1 is equal to str2 Can someone explain this?
2 odpowiedzi
+ 2
When it comes to Strings, the == operator in Java checks for equality of *reference*, not of *value*.
What this means is that it's checking if str1 and str2 are the *same object*, which they're not. Their values just happen to be the same ("test").
You can use .equals() or Objects.equals() to actually compare the values in str1 and str2.
Check this link for a nice little reference:
https://stackoverflow.com/a/513839/10892645