Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String creation and String constant pool

When a String is created using the keyword new it creates a new String object using a constructor that takes a String literal. I'm wondering if the literal get stored in the constant pool before the String constructor is called.

The reason I'm asking is that in "OCA Java SE 7 Programmer I Certification Guide", Mala Gupta writes:

public static void main(String[] args)
{
    String summer  = new String("Summer");   //Line 1: The code creates a new String object with the value "Summer". This object is not placed in the String constant pool.
    String summer2 = "Summer"                //Line 2: The code creates a new String object with the value "Summer" and places it in the String constant pool.
}

She says on the first line that the String object that is created by new is not stored in the constant pool. This is fine, but what is not clear is if the literal "Summer" that goes in the constructor on the first line is.

On the second line she says that the assignment of "Summer" to summer2 stores it in the constant pool, which implies that the literal on the first line was not placed in the pool.

My Question

  1. Line 1: Does the literal "Summer" in the constructor get placed in the constant pool before the String constructor is called?
  2. Line 2: Does "Summer" already exist in the pool at line 2 or is it inserted at this line?
  3. Line 2: Is the author wrong when she says that "Summer" is inserted in the pool at line 2?
like image 416
Adam Avatar asked Jul 23 '14 12:07

Adam


People also ask

What is the string constant pool in Java?

String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String class. By default, the String pool is empty.

How many strings are getting created in the string pool?

The answer is: 2 String objects are created. str and str2 both refer to the same object.

What makes Java more memory efficient string or string pool?

To make the java more memory efficient the concept of string literal is used. By the use of 'new' keyword, the JVM will create a new string object in the normal heap area even if the same string object present in the string pool.

Does new string Use string pool?

Conclusion: new String() will always create a new object and string literal checks the string pool before creating one.


2 Answers

In short and without confusion,

You wrote ,

   String summer  = new String("Summer");   //Line 1: The code creates a new String object with the value "Summer". This object is not placed in the String constant pool.

That is wrong. Especially the comment >This object is not placed in the String constant pool.

The string literal "Summer" is in pool now. And the object summer created in heap.

Does the literal "Summer" in the constructor get placed in the constant pool before the String constructor is called?

Yes. It goes to pool as it is a String literal.

Does "Summer" already exist in the pool at line 2 or is it inserted at this line?

No. Two literals are there since you are not interned. (read more about String interning)

Is the author wrong when she says that "Summer" is placed in the pool at line 2?

Other is correct with that line.

For remembrance, we can even simply say that everything between "" goes in to pool regardless of where it is using.

like image 99
Suresh Atta Avatar answered Oct 26 '22 04:10

Suresh Atta


Code 1 - Line 1: Does the literal "Summer" in the constructor get placed in the constant pool before the String constructor is called?

Yes. The JLS states:

This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

Since that string is a literal (and therefore the value of a constant expression), it will be inserted. However, the object assigned to the variable summer is not that same literal, it is explicitly a new string object created to copy the value of that literal.

Code 1 - Line 2: Does "Summer" already exist in the pool at line 2 or is it inserted at this line?

As above, it is already inserted.

Code 2 - Line 2: Is the author wrong when she says that "Summer" is placed in the pool at line 2?

Nope - though I agree the wording could have been clearer.

like image 1
Michael Berry Avatar answered Oct 26 '22 03:10

Michael Berry