I was reading about comparison of String Objects in java using == operator.
class Teststringcomparison3{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
It says The == operator compares references not values.
But when I tried printing the reference for all three strings it turned out to be same.
public class StringComparison {
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(Integer.toHexString(s1.hashCode()));
System.out.println(Integer.toHexString(s2.hashCode()));
System.out.println(Integer.toHexString(s3.hashCode()));
}
}