The reason is that in the first example, the string "helloworld"
is already in the string pool, on account of its being the name of the class. So interning it doesn't add anything to the string pool. So str
won't be the interned value, and the comparison will be false.
In the second example, str.intern()
actually adds str
to the string pool, because "helloworld"
is not already there. Then, when the "helloworld"
literal is encountered, the string object that's actually used is the one that's in the string pool. That's just str
, so the comparison will be true.