Originally posted by Sergey Elberg:
Tell me pls why in following code
String s1=new String("Java");
String s2=new String("Java");
s2=s2.intern();
System.out.print(s1==s2);
the result is FALSE. but in
String ss1="Java";
String ss2=new String("Java");
ss2=ss2.intern();
System.out.print(ss1==ss2);
the result is TRUE.
Thanks
In first case you are using keyword new to create String s1 and s2, this creates 2 different objects in memory. When you do s2.intern() it returns the String from String pool but String s1 is still unchanged, hence it returns false.
In case 2, you are not using keyword new to create s1, so s1 points to string in string pool and s2.intern() returns string from string pool and thus it returns true.
[ April 04, 2006: Message edited by: Jay Ashar ]