String s4
is interned and reference to this string and to "hellohello" is the same.
Because of that you get true
on the line:
System.out.println("hellohello" == s4);
String s6
is not interned, it depends on a variable s2
. And references to "hellohello" and to string s6
are not equal. Because of that you get false
on the line:
System.out.println("hellohello" == s6);
But if you declare s2
as final, which makes s2
constant, you will get true
instead of false
on the line System.out.println("hellohello" == s6);
, because now compiler can intern the string s6
, as it depends on constant values, and the references to "hellohello" and to s6
will be equal to each other.