I know that java stores String Literals
in Common Pool
and two string literals having the same text will refer to the same place in Common Pool. take below code:
String str1 = "Amir";
String str2 = "Amir";
now both str1
and str2
refer to the same place in the Common Pool. so from what all we know we must use equals() to properly compare these two strings and obviously str1.equals(str2)
will be true
.
now from what i read here it says that the str1 == str2
will be true becuase the strings both have the same address (sounds pretty logical) but it also states that its a logical error to do so.
my question is what is that special case that may cuase trouble and inconsistency to my code if I use str1 == str2
?