1

I read that string constant pool is self referenced Also in this link it is written as the creation of String literal :

String s= "new";

will create a new String "new" in the heap if there is not one. So does it mean that object is always created in the heap regardless its literal or new object using new keyword?

What i understood of intern is -- it checks if there is a object in the heap with same name then it is referenced else new object is created in the heap. Please correct if i am wrong here.

Another doubt i have is - does the constant pool contains the objects or just the refernces to the objects in the heap.

Community
  • 1
  • 1
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41
  • Does't the question you linked to already answer your quesitons? – Chetan Kinger Mar 14 '15 at 15:08
  • yeah , but that link also says that a while creating a object using new is also checked whether there is a object with same value in the name, which i believe is not since new always creates a new object in the heap. So i just want to assure what i have understood is correct. – jayendra bhatt Mar 14 '15 at 15:43

2 Answers2

3

does it mean that object is always created in the heap regardless its literal or new object using new keyword?

Yes, in Java all Object-derived objects, including Strings, are created in the heap. The only difference is that identical String objects from the constant pool get reused with the help of the compiler, while String objects created with operator new require explicit code from the programmer in order to get reused.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • kk got that ...does the constant pool contains the objects or just the refernces to the objects in the heap. – jayendra bhatt Mar 14 '15 at 17:27
  • @jayendrabhatt All objects, including the pool itself, are in the heap. Java lets you embed scalar primitive values, but as soon as you get into arrays or other objects, all you get is a reference+an object in the heap. – Sergey Kalinichenko Mar 14 '15 at 17:34
  • i am not very much sure whether literal pool is part of heap as this [link](http://blog.jamesdbloom.com/JVMInternals.html#constant_pool) tells it is not but as you said the pool will just contain a reference with correponding object in the heap. – jayendra bhatt Mar 14 '15 at 17:43
  • @jayendrabhatt The author of the post differentiates between heap objects that are created with `new` and objects that are members of the "permanent generation", which he does not call heap objects. This may be useful for the purposes of discussing Java memory management, but it does not make permgen objects any different from heap objects. Moreover, apparently Java-8 eliminated permgen altogether. – Sergey Kalinichenko Mar 14 '15 at 18:10
0

Yes

it is on heap.

and with respect to intern()             

Yes

you are right.
KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44