In your edited question, you have defined two String literals. As per JLS § 3.10.5 these literals are interned:
a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
Therefore reference equality using ==
will always return true
for s1 == s2
where:
String s1 = "string";
String s2 = "string";
The JLS states (almost exactly) the same for Java 7 and Java 6
Note though that there are some subtle differences in the way string interning works between certain JDK versions. See, for instance "intern() behaving differently in Java 6 and Java 7"