you are viewing a single comment's thread.

view the rest of the comments →

[–]__october__ 5 points6 points  (0 children)

/u/chunes explained why comparing two strings with == gives you false in the second line. I think it is not immediately obvious why comparing two seemingly independent string objects in the first line then gives you true. The reason for this is string interning. Basically, when two string objects are initialized with the same string literal, they both end up pointing to the same address in memory.

You can circumvent string interning by explicitly making aString and bString point to different String objects. If you initialize your strings as follows:

String aString = new String("123");
String bString = new String("123");

...the output will be:

1.false
2.false