12

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?
Adam
  • 364
  • 2
  • 11
  • http://stackoverflow.com/questions/14150628/string-constant-pool-java – ZaoTaoBao Jul 23 '14 at 12:47
  • possible duplicate of [Is String Literal Pool a collection of references to the String Object, Or a collection of Objects](http://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-co) – naveejr Jul 23 '14 at 13:02
  • Have a look http://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-co – naveejr Jul 23 '14 at 13:02
  • @naveejr Not a duplicate but definitely helps :) Thanks. – Adam Jul 23 '14 at 13:30

4 Answers4

10

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • @Suresh If "Summer" is inserted at line 1 and already exists at line 2, then isn't the author is wrong? – Adam Jul 23 '14 at 12:53
  • @Adam If author said it is not inserted in pool, when you used in ocrrect, then author is wrong. Your comment at line.1 you said it's not placed. In your questions list you said placed in the pool. Can you cross check and comment again. I'm little confused. – Suresh Atta Jul 23 '14 at 12:57
  • @sᴜʀᴇsʜ The comments in the code are what the author wrote. My question is "If the literal at line 1 is inserted in the pool, is the author wrong by saying that it is inserted at line 2?" Hope this is clearer :) – Adam Jul 23 '14 at 13:02
  • If author said it inserted only at line 2 then wrong. Inserted at both lines. – Suresh Atta Jul 23 '14 at 13:05
  • @sᴜʀᴇsʜᴀᴛᴛᴀ - can you prove the same via code?. I have been asked this question, I answered the same as you did, and the person who asked was like - *can you prove it*? – TheLostMind Jul 23 '14 at 15:11
  • @TheLostMind Which question you want to prove ? – Suresh Atta Jul 23 '14 at 15:36
  • @sᴜʀᴇsʜᴀᴛᴛᴀ - Programatically, (or by using some performance analysis tool or even a decompiler), can it be proved that in `new String("abc")`, `abc` is *interned* automatically?. Ya, theoretically, it makes sense, but how can I prove it?de – TheLostMind Jul 24 '14 at 05:17
1

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.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
-1
**Code 1 - Line 1: Does the literal "Hello" in the constructor get placed in the constant pool before the String constructor is called?**

Yes, because constructor is used to instantiate the object.

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

**it is already there **. it is not inserted by this line. you can check by like this.

       String s=new String("hello");
String s1=s.intern();
String s2 = new String("hello");
String s3= s2.intern();

if (s1==s3)
{
System.out.println("true");
} if (s==s2)
{System.out.println("false");}

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

NO. summer will not be placed at the constant pool because it is already there code2-line1 will place "summer" there.

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • i knew some people will down vote for this answer. literals place the string directly into the constant pool (string s). and string s1 will make an object as well as check weather the string "hello" is there in the constant pool. and as "hello" was placed in the constant pool before by the literal method. so true will be printed and we will know that it was there. – Deepanshu J bedi Jul 23 '14 at 13:00
  • 2
    Not at all. `new String()` will always create a new object, so this prints `false`. – Keppil Jul 23 '14 at 13:02
  • thumbs up. i forgot about the intern method. thank you for the down votes. – Deepanshu J bedi Jul 23 '14 at 13:07
-3
    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.

This statement is wrong. ABove line will create 2 object one is in Constant pool and other one is in heap.Whenever you will write "ABC" , it will go to constant pool no matter inside constructor or not.

Proof

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
String s4 = new String("Rakesh");
String s5 = new String("Rakesh").intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
}

if ( s1 == s4 ){
    System.out.println("s1 and s4 are same" );  // 3.
}

if ( s1 == s5 ){
    System.out.println("s1 and s5 are same" );  // 4.
}will return:

s1 and s2 are same
s1 and s3 are same
s1 and s5 are same
Arun
  • 1,038
  • 2
  • 12
  • 25
  • 4
    No.. He will *not* get the answer he wants from the above code. And , *I think once you will run above code , you will answer of all three question* is a very bad way of answering.. – TheLostMind Jul 23 '14 at 12:48
  • Apologies for my mistake – Arun Jul 23 '14 at 17:47