This question already has an answer here:
What is the difference between
String str = new String("abc");
and
String str = "abc";
This question already has an answer here:
What is the difference between
String str = new String("abc");
and
String str = "abc";
In addition to the answers already posted, also see this excellent article on javaranch.
A String literal is a Java language concept. This is a String literal:
A String object is an individual instance of the
java.lang.String
class.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:The long answer is available here, so I'll give you the short one.
When you do this:
You are calling the
intern()
method on String. This method references an internal pool ofString
objects. If the String you calledintern()
on already resides in the pool, then a reference to thatString
is assigned tostr
. If not, then the newString
is placed in the pool, and a reference to it is then assigned tostr
.Given the following code:
When you check for object identity by doing
==
(you are literally asking: do these two references point to the same object?), you gettrue
.However, you don't need to
intern()
Strings
. You can force the creation on a newObject
on the Heap by doing this:In this instance,
str
andstr2
are references to differentObjects
, neither of which have been interned, so that when you test forObject
identity using==
, you will getfalse
.In terms of good coding practice: do not use
==
to check for String equality, use.equals()
instead."abc"
is a literal String.In Java, these literal strings are pooled internally and the same String instance of
"abc"
is used where ever you have that string literal declared in your code. So"abc" == "abc"
will always be true as they are both the same String instance.Using the
String.intern()
method you can add any string you like to the internally pooled strings, these will be kept in memory until java exits.On the other hand, using
new String("abc")
will create a new string object in memory, which is logically the same as the"abc"
literal."abc" == new String("abc")
will always be false, as although they are logically equal they refer to different instances.Wrapping a String constructor around a string literal is of no value, it just needlessly uses more memory than it needs to.
There is a subtle differences between String object and string literal.
In this simple case, "abc" will go in the pool and s will refer to it.
In this case, because we used the
new
keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.In the first case, there are two objects created.
In the second case, it's just one.
Although both ways
str
is referring to"abc"
.