Possible Duplicate:
How do I compare strings in Java?
String s1 = "andrei";
String s2 = "andrei";
String s3 = s2.toString();
System.out.println((s1==s2) + " " + (s2==s3));
Giving the following code why is the second comparison s2 == s3 true ?
What is actually s2.toString() returning ? Where is actually located (s2.toString()) ?
First of all
String.toStringis a no-op:Second of all, String constants are interned so s1 and s2 are behind the scenes changed to be the same String instance.