A String literal is a Java language concept. This is a String literal:
"a String literal"
A String object is an individual instance of the java.lang.String
class.
String s1 = "abcde";
String s2 = new String("abcde");
String s3 = "abcde";
All are valid, but have a slight difference. s1
will refer to an interned String object. This means, that the character sequence "abcde"
will be stored at a central place, and whenever the same literal "abcde"
is used again, the JVM will not create a new String object but use the reference of the cached String.
s2
is guranteed to be a new String object, so in this case we have:
s1 == s2 // is false
s1 == s3 // is true
s1.equals(s2) // is true