String s1 = "java";
String s2 = "java";
System.out.println(s1==s2); //true
System.out.println(""+s1==s2); //false
why the output is so?
in the second case only added "" quotes.
String s1 = "java";
String s2 = "java";
System.out.println(s1==s2); //true
System.out.println(""+s1==s2); //false
why the output is so?
in the second case only added "" quotes.
==
compares references of two string objects which will be different for the two cases above.String pool
will save some memory by regrouping to the same place in memory different String objects with the same value.Look at the following code to understand how it works:
String s1 = "java";//first string
String s2 = "java";//thanks to string pool the s2 is actually using the same memory location as s1 (saving space for same value string objects)
System.out.println(s1==s2); //true ref comparison equals thanks to string pool intervention
System.out.println(""+s1==s2); //false (the resulting string of ""+s1 is stored in a different place in memory than s4
System.out.println(""+(s1==s2));//true due to string pool
System.out.println(s1.equals(s2)); //true -> value comparison
System.out.println(s2.equals(""+s1)); //true -> value comparison
String s3 = new String("java");//this construction does not let the string pool intervene
String s4 = new String("java");//2 different memory locations for those 2 objects
System.out.println(s3==s4); //false no string pool intervention -> 2 different memory locations
System.out.println(""+s3==s4); //false false (the resulting string of ""+s3 is stored in a different place in memory than s4
System.out.println(""+(s3==s4));//false no string pool intervention -> 2 different memory locations
System.out.println(s3.equals(s4)); //true -> value comparison
System.out.println(s4.equals(""+s3)); //true -> value comparison