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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@Suresh If "Summer" is inserted at line 1 and already exists at line 2, then isn't the author is wrong?
@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.
@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 :)
If author said it inserted only at line 2 then wrong. Inserted at both lines.
@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?
|
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.

Comments

-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.

3 Comments

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.
Not at all. new String() will always create a new object, so this prints false.
thumbs up. i forgot about the intern method. thank you for the down votes.
-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

2 Comments

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..
Apologies for my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.