2

I have seen many questions regarding object created using string literal and new keyword like:

How many String objects using new operator

But it doesn't clarify my doubts.

Case 1: String object using string literal. It creates one object in string constant pool if,it is not present otherwise, return the reference of this object.This object is implicitly interned.

Case 2:String object using new(). it creates 2 objects,one in string constant pool and another one in heap area.Reference variable refer to the heap area object.For this object we need to call intern method to put this object into string constant pool explicitly.

My question is if new() already creates one object in string constant pool then, what is use of calling intern method on the object which is there in heap area?

Gajanan Kulkarni
  • 697
  • 6
  • 22
  • 3
    You got it wrong: `new` does *not* create an object in the string-pool, it creates only one object, on the heap. – Nir Alfasi Dec 20 '14 at 08:05
  • Please check the answer:http://stackoverflow.com/questions/9552218/how-many-string-objects-using-new-operator – user3611241 Dec 20 '14 at 08:09

2 Answers2

2

Case 2:String object using new(). it creates 2 objects,one in string constant pool and another one in heap area.

Only if you create a new String object by passing it a string literal, like this:

String s = new String("hello");

The literal "hello" will cause an object in the string constant pool to be created. The new String will create a new String object on the heap, with a copy of the content of the object for the literal.

You should never create String object like that, because it's unnecessary and inefficient.

There are however other reasons why you would want to do new String(...), when the value that you pass to the constructor is not a string literal. For example, the value is data read from a file.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Yes i agree with the answer but my question is what is use of calling intern() method on object "s" if the literal is already there in string constant pool. – user3611241 Dec 20 '14 at 08:18
  • 2
    There's no use and you should not do this. You should just write `String s = "hello";` instead. Doing something like `String s = new String("hello").intern();` is useless and unnecessarily inefficient. – Jesper Dec 20 '14 at 08:20
0

Case 1: String object using string literal. It creates one object in string constant pool

Correct.

if,it is not present

Wrong. It is present.

otherwise, return the reference of this object.

It always return the reference of the object. No 'otherwise' about it.

This object is implicitly interned.

Not really. It is already interned, because it is a string literal. The compiler and class loader see to that. Not thenew operator.

Case 2:String object using new(). it creates 2 objects,one in string constant pool

Not really. It was already there: see above.

and another one in heap area.

Correct.

Reference variable refer to the heap area object.For this object we need to call intern method to put this object into string constant pool explicitly.

Correct.

My question is if new() already creates one object in string constant pool

It doesn't. See above.

user207421
  • 305,947
  • 44
  • 307
  • 483