4

When we use new operator to create a String object, I read that two Objects are created one Object is string constant pool and second one is in heap memory.

My question here is We are using new operator hence only one object should be created in Heap. Why then one more object has to be created in String Constant pool. I know Java stores String object whenever we not use new operator to create a String. For eg:

String s = "abc" . 

In this case only it will create in String constant pool.

String s2 = new String("abc") 

only one object hast to be created in heap and not in Constant pool.

Please explain why I am wrong here.

Oleks
  • 31,955
  • 11
  • 77
  • 132
user900721
  • 1,417
  • 4
  • 17
  • 29
  • 1
    Where did you read this? – CodeBlue Mar 04 '12 at 04:28
  • Yes, I read in Kathy Sieera book...it says when we create String using new operator ...like String s = new String("abc") In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal "abc" will be placed in the pool. – user900721 Mar 04 '12 at 07:22
  • You may want to have a look at the post http://stackoverflow.com/questions/1881922/questions-about-javas-string-pool which covers your query. – Kuldeep Jain Mar 04 '12 at 09:31

7 Answers7

11

We are using new operator hence only one object should be created in Heap.

Sure - the new operation only creates one object. But its parameter is a String literal, which already represents an object. Any time you use a String literal, an object was created for that during class loading (unless the same literal was already used elsewhere). This isn't skipped just because you then use the object as a parameter for a new String() operation.

And because of that, the new String() operation is unnecessary most of the time and rarely used.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2

It will be created two object, one object into pool area and other one in non pool area because you are using new and as well as string literal as a parameter.

String s = new String("abs");
Ajit Kumar Dubey
  • 1,383
  • 1
  • 19
  • 33
2
String s2 = new String("abc")

It will create only one object in heap memory. We need to use intern() method of java.lang.String explicitly to make the entry in String pool.

String s = "def".

Two Objects will be created here. 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.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
2

See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28

Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern

Thus when you write

String s2 = new String("abc")

The Compile-time constant expression "abc" will be interend.

Oded Peer
  • 2,377
  • 18
  • 25
1

String in java is Immutable, once created its cannot be changed. also, any string literal will be stored in the string pool for later use whenever the same literal is used again, for example:

String name = "my name"; // one intern object is created in pool
String otherName = "my name"; // the old intern object is reused
System.out.println( name == otherName); // true , the same reference refer to same object

the two reference refer to the same location in the pool.

String name = new String("my name"); // one object is created, no string pool checking
String otherName = new String("my name"); // other object is created, no string pool checking
System.out.println( name == otherName); // false, we have 2 different object

here we have two different String object in memory each of them have its own value.

see this article for more: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

Wajdy Essam
  • 4,280
  • 3
  • 28
  • 33
  • ya I agree, but my question is different. In above example, how many objects are created for the line String otherName = new String("my name"); ? – user900721 Mar 04 '12 at 08:23
  • one object per each new String , JVM doesn't check string pool when creating String using new operator. check the updated comments. – Wajdy Essam Mar 04 '12 at 08:58
  • This process is commonly called string interning, by the way. – Jordan Mar 04 '12 at 08:58
  • Your answer is actually not entirely correct. **new String("my name");** actually created 2 objects. You can test this by first assigning **"my name"** to a variable and constructing the string with that variable, then compare them. – M Platvoet Mar 04 '12 at 09:16
0

form this below line of code will create 3 object in the JVM

String s2 = new String("abc")
  1. for "abc" in String pool memory.
  2. for new operator one object in heap.
  3. one more for s2.
Gimby
  • 5,095
  • 2
  • 35
  • 47
Ravikumar
  • 9
  • 1
-1

String s2 = new String("abc");
here 1 literal and 1 object will be created.

With new operator 1 String object will be created in heap as "abc" and s2 will refer it, moreover "abc" is string literal also that is passed in String Constructor so it will go in String Constant pool.

literal is consider as object so 2 object will be created here.

Vikas
  • 490
  • 4
  • 10