Scenario 1
public static void main(String[] args) throws Exception {
String s1 = "Testing";
String s2 = new String("Testing");
s2.intern();
if(s1==s2){
System.out.println("s1 equals to s2");
}else{
System.out.println("s1 is not equal to s2");
}
}
OUTPUT: s1 is not equal to s2
.
Scenario 2
public static void main(String[] args) throws Exception {
String s1 = "Testing";
String s2 = new String("Testing").intern();
if(s1==s2){
System.out.println("s1 equals to s2");
}else{
System.out.println("s1 is not equal to s2");
}
}
OUTPUT: s1 equals to s2
.
My question is what is the difference between new String("Testing")
and new String("Testing").intern()
?