There's an optimization the compiler makes called the String Constant Pool.
Any Strings with Constant literals are placed here.
In your example,
String s1 = "abc";
The String literal "abc" will be placed in the pool.
Everytime the literal "abc" is used, instead of creating a new object the string will point to the memory location of the string in the pool.
String a = "abc";
String b = "abc";
These variables point to the EXACT same objects.
It's like writing:
Object d = new Object();
Object e = d;
As Nikhil Sharma
's said, == compares the value of the memory location of objects. As Donna
said, a String is a class.
== will result in true; they are the same obects.
But, there's a catch.
When using the new keyword a String will not be interned unless done manually. In other words, a new String will be created instead of checking the SCP for the same string values.
String a = new String("b");
String b = new String("b");
So, these are different objects.