Also if
String s3 = "abcd"
then
(s1 == s3) returns true and
(s1.equals(s2)) returns true?
' == ' Operator determines whether the variables refrence the same object in memory and returns true only if references are to same object.
if you construct two Strings with same literal without using new keyword ie
String a = "hello";
String b = "hello";
if(a == b) // this will return true.
String and Wrapper classes override Object class's equals() method.
' equals() ' method compares the contents of the objects and checks for content equality.
eg: String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s3.equals(s4)); // true
System.out.println((s3==s4)); //false
Long l = new Long(100);
Long lol = new Long (100);
System.out.println(l.equals(lol));
Note: StringBuffer do not override equals() method whereas String class does.
Hope this helps
-Bani