I was asked to predict the output of this code :
String party1 = "party";
String party2= "PARTY".toLowerCase();
if(party1==party2){
System.out.println("EQUAL");
}else{
System.out.println("NOT EQUAL");
}
I thought the result of toLowerCase()
would be interned automatically and hence the same reference to party1
would be returned, but I was wrong since the code ouptuts "NOT EQUAL".
Does toLowerCase()
(toUpperCase()
as well) return a new String
?
Edit : I am not using == to compare Strings, I just want to know about the behaviour of toLowerCase() method