Reputation: 5042
Consider following code:
String s = "Dear"; String s1 = "My"+ s; String s2 = "MyDear"; String s3 = "My"+"Dear";
Here s1 and s3 are created by string constant expressions.
System.out.println(" s2 == s3 is " + (s2 == s3)); //true
comes out to true because both s2 and s3 point to same interned String object but
System.out.println(" s1 == s2 is " + (s1 == s2)); //false
comes out to fasle. Why?
Upvotes: 0
Views: 81