Okay, so two responses to this. First, the ethically correct one, do never test strings with ==
, always use .equals()
or .equalsIgnoreCase()
.
Secondly, it's true that indeed, "a" == "a"
because the strings are stored in, as you call it, the same pool. The problem here is that you append to it. Appending to a string causes it to become a different string, which is not stored in the string pool. The string pool is only generated on-compile, and as the second string is calculated on runtime, it won't match the one generated on-compile.
Imagine a string-pool to work like this:
a = "test";
b = "te";
c = "st";
d = "test";
The compiler translates this into
sp1 = "test";
sp2 = "te";
sp3 = "st";
a = sp1;
b = sp2;
c = sp3;
d = sp1;
Now ==
will check if two variables refer to the same sp. If you run b + c
java will not go back and check if any of the sp's is the same as that. It only does that on compile.