Reputation: 225
String a = "abc";
This creates literal "abc"
in string pool.
String b = "abc";
No new literal is created. b
is pointed to the existing "abc"
.
String c = new String("abc");
Now the object is created in heap and c
is pointed to the heap. The literal is also created in the string pool.
But, what happens if the pool already has the literal "abc"
? Will there be duplicate literals in the pool?
Upvotes: 5
Views: 182
Reputation: 421310
But, what happens if the pool already has the literal "abc"? Will there be duplicate literals in the pool?
No.
String c = new String("abc");
is semantically equivalent to
String tmp = "abc";
String c = new String(tmp);
By this it should be clear that no extra entry in the string pool is created just because the literal is used as argument to the string constructor.
You are right about the fact that new String(...)
creates a string on the heap, so looking at both heap and string pool, there will be multiple objects representing "abc"
, but no more than one in the string pool.
[...] That is when we use string literals. But will the literal in the string pool be reused if we give,
String a = new String("abc");
?
Yes, the string pool literal will be reused if you give "abc" as argument to the constructor. The resulting String object will however not be in the pool, but on the heap.
Upvotes: 6
Reputation: 1743
String c = new String("abc");
Now the object is created in heap and c is pointed to the heap. The literal is also created in the string pool.
Literal value won't be created into string pool. It will remain in heap only. See this picture.
http://www.journaldev.com/797/what-is-java-string-pool
But, what happens if the pool already has the literal "abc"? Will there be duplicate literals in the pool?
There can't be duplicate values in pool. As strings are immutable object in java. See the second answer in this post by roger_that.
String is immutable. What exactly is the meaning?
Upvotes: 0
Reputation: 311054
The string literal "abc"
is already in the string pool, before any of this code executes. It is placed there by the compiler and classloader. It doesn't happen during execution of this code.
Upvotes: 0
Reputation: 26077
There will not be duplicate entry in String pool, it will be re-used by another reference.
String s1="foo";
literal will go in pool and s1 will refer.
String s2="foo";
this time it will check "foo" literal is already available in StringPool or not as now it exist so s2 will refer the same literal.
String s3=new String("foo");
"foo" , through string arg constructor String Object will be created i.e "foo" in the heap due to object creation through new operator then s3 will refer it.
String interning using inter() method
Java by default doesn't put all String object into String pool, instead they gives you flexibility to explicitly store any arbitrary object in String pool. You can put any object to String pool by calling intern() method of java.lang.String class. Though, when you create using String literal notation of Java, it automatically call intern() method to put that object into String pool, provided it was not present in the pool already.
Upvotes: 0
Reputation: 62884
But, what happens if the pool already has the literal "abc"?
It will simply be reused.
If you somewhere do String x = "abc";
and somewhere else String y = "abc";
then x == y
will always be true
. This proves that actually the literal is re-used.
Upvotes: 3