N
newvb
Here i wrote small code to test the different between equals and "=="
public class TestString {
public static void main(String[] args){
String s = "Hello World";
String s1 = new String("Hello World");
String s2 =s.intern();
String s3 = "Hello World";
System.out.println("s == s1: " + (s==s1));
System.out.println("s == s2: " + (s==s2));
System.out.println("s == s3: " + (s==s3));
System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());
}
}
However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false
For the last output s==s1 is missed. Does anyone have some idea about
this?
Thanks
public class TestString {
public static void main(String[] args){
String s = "Hello World";
String s1 = new String("Hello World");
String s2 =s.intern();
String s3 = "Hello World";
System.out.println("s == s1: " + (s==s1));
System.out.println("s == s2: " + (s==s2));
System.out.println("s == s3: " + (s==s3));
System.out.println("s equal s1: " + s.equals(s1));
System.out.println("s == s1: " + s1.intern()==s.intern());
}
}
However, I got some strange output as follows:
s == s1: false
s == s2: true
s == s3: true
false
For the last output s==s1 is missed. Does anyone have some idea about
this?
Thanks