a Learner
a Learner

Reputation: 5042

String created by constant expressions

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

Answers (1)

user207421
user207421

Reputation: 310883

s1 is not created by a constant expression. s3 is.

Upvotes: 4

Related Questions