String a = "devender";
String b = "devender";
String c = "dev";
String d = "dev" + "ender";
String e = c + "ender";
System.out.println(a == b); //case 1: o/p true
System.out.println(a == d); //case 2: o/p true
System.out.println(a == e); //case 3: o/p false
a & b both are pointing to the same String Literal in string constant pool. So true
in case 1
String d = "dev" + "ender";
should be internally using something like:
String d = new StringBuilder().append("dev").append("ender").toString();
How a & d are pointing to the same reference & not a & e ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…