5
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?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Divya Rose
  • 227
  • 4
  • 22

5 Answers5

6

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • `String c=new String("foo");` will create a string in heap and not in pool if you call `intern()` then it will be a part of pool – singhakash Jun 15 '15 at 10:55
  • 2
    I never said the opposite. Instead of posting this as a comment to multiple existing answers, I suggest you post an answer of your own. (Besides, while `intern()` is related to strings and the string pool, I don't see how it is relevant for this particular question.) – aioobe Jun 15 '15 at 11:00
3

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.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 1
    ok... 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"); – Divya Rose Jun 15 '15 at 10:56
0

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.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 2
    `String s3=new String("foo");` will create a string in heap and not in pool if you call `intern()` then it will be a part of pool – singhakash Jun 15 '15 at 10:53
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0
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?

Community
  • 1
  • 1
kingAm
  • 1,755
  • 1
  • 13
  • 23