Since String are immutables, a useful implementation of the toString method is - in the String class - to return this.
As an example, my rt.jar contains the following implementation :
public String toString() {
return this;
}
As a consequence, the reference associated to s3
is the same than the one associated to s2
.
Considering the s1==s2
statement, it is due to the automatic call to intern()
for all constant Strings. That means that at compile time, s2 initialization code will be replaced by s2=s1
, which makes the assertion quite obvious, no ?