It behaves as it should. It is adressed in two sections of the JLS.
strings that are the values of constant expressions (ยง15.28) – are “interned” so as to share unique instances, using the method String.intern.
JLS #15.28 lists what is considered as a constant expression. In particular, string literals are constant expressions (“Hel” and “lo”) but for a variable to be considered a constant, it needs to be final.
In your case, if you change your code slightly to make str8
and str9
constant, you will get true
three times:
final String str8 = "Hel";
final String str9 = "lo";