2

When a String is created using the keyword new it creates a new String object using a constructor that takes a String literal.

Does the literal get stored in the constant pool before the String constructor is called?

String hello = new String("Hello");
String literal = "Hello";             //Does "Hello" already exist in the pool
                                      //or is it inserted at this line?

EDIT

In "OCA Java SE 7 Programmer I Certification Guide", Mala Gupta writes:

public static void main(String[] args)
{
    String summer  = new String("Summer");   // The code creates a new String object with the value "Summer". This object is not placed in the String constant pool.
    String summer2 = "Summer"                // 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 interned.

Adam
  • 364
  • 2
  • 11

3 Answers3

5

Regardless of where you are using, all string literals saves in String pool. So the answer is YES.

String hello = new String("Hello");
                        >--------<  goes to pool.

But the thing is that the h2 won't refer from that h :)

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    @CraigOtis What do you mean by that. I'm just giving example for my last statement. – Suresh Atta Jul 16 '14 at 11:38
  • @Suresh Can you clarify what it is that you have marked as goes in the pool? Is it the object created by the new operator? Or is it the literal in the constructor? Because that is what I'm after: Does the literal in the constructor go in the constant pool? – Adam Jul 16 '14 at 11:52
  • @Adam Object `hello` created at heap and the literal `"Hello"` goes in to String pool. There is a clear difference between Objects reference and the String literal. – Suresh Atta Jul 16 '14 at 11:53
  • @Suresh Does that mean that the book that I wrote about in my edit is wrong? – Adam Jul 16 '14 at 11:59
  • @Adam She said right. The object stored in the heap and the String literal used there is not interned. Please refer to String interning. – Suresh Atta Jul 16 '14 at 12:03
  • @Suresh My original question was "Does the literal get stored in the constant pool before the String constructor is called?", and you answered "YES". Now you are saying that the literal used in the constructor is not interned. I'm a bit confused :p – Adam Jul 16 '14 at 12:16
  • @Adam Sorry for that confusion. 1) Going in to pool is different thing from interning. I'm still saying that the literal used in the constructor goes in to pool. Do no confuse. – Suresh Atta Jul 16 '14 at 12:22
  • So the statement **new String("Rabbit");** creates two String objects, one of them goes in the pool. – Adam Jul 16 '14 at 12:39
  • @Adam Nope. An Object created and the String literal (it's not an object) goes in to pool. – Suresh Atta Jul 16 '14 at 12:43
  • @Adam Am I failed to answer your questions ? :) – Suresh Atta Jul 23 '14 at 13:06
  • @Suresh No, but this question got kind of messy and I tried to delete it. SO wouldn't let me anyway, so I'll give it back :) – Adam Jul 23 '14 at 13:12
1

When you write "Hello" in your code, this String is created during compilation. so actually its already there since you also used it to create your new String("Hello"); with the "Hello" in it. in conclusion: Yes.

Dima
  • 8,586
  • 4
  • 28
  • 57
0
String ob1 = new String("Hello");
String ob2 = "Hello"; 

First line first seeks a "Hello" string in String Pool, if it is there it creates same in Heap and our ob1 refers to that heap object. If it is not there it creates same in Pool also and in this case also, ob1 refers to heap object. Second line also seeks for "Hello" object in pool if its found ob2 will refer to that pool object. If not found it will create only in Pool and ob2 will refer to that pool object. But second line never creates an String object in heap. String objects kept in String Pool are reusable but String objects created in heap are not.

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28