File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constant pool or heap

 
ilias basha
Ranch Hand
Posts: 55
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the Strings are stored in String constant pool, i have the following question

* if we use new String("xyz"); - will this xyz still be part of String Constant Pool or just part of HEAP as any other object does?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which "xyz"? You have two there.
 
ilias basha
Ranch Hand
Posts: 55
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I mean xyz in the following statement
new String("xyz")
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All objects are on the heap. String objects that are in the String constant pool are just normal String objects, so they are also on the heap.

As Campbell hints, you have two String objects there: one for the literal "xyz" and another one that you create explicitly (with new String(...)). String literals will be registered in the constant pool of strings (but they are also just String objects on the heap!).

So to answer your question:
ilias basha wrote:... will this xyz still be part of String Constant Pool or just part of HEAP as any other object does?

Both.
 
ilias basha
Ranch Hand
Posts: 55
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for both of you
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
As Campbell hints, you have two String objects there: one for the literal "xyz" and another one that you create explicitly (with new String(...)). String literals will be registered in the constant pool of strings (but they are also just String objects on the heap!).


what deos it mean by one for the literal "xyz" i understand the new String("xyz") object but what is the 2nd one there ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, String constant pool is collection of references, not an objects. Objects always stay in heap.

when you say new String("xyz") :
for "xyz" literal-----> *xyz*[say id = 1] created in heap and refernce[craeted in String constant pool] ponting to the object.

new String("xyz"); -> when you say *new*, an new Object *xyz*[say id = 2] created which is meaningfully equalent to "xyz" in heap
and here reference for this object *wont* be in String constant pool.

is it make sense?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote:
Jesper Young wrote:
As Campbell hints, you have two String objects there: one for the literal "xyz" and another one that you create explicitly (with new String(...)). String literals will be registered in the constant pool of strings (but they are also just String objects on the heap!).


what deos it mean by one for the literal "xyz" i understand the new String("xyz") object but what is the 2nd one there ?


Read here: Strings, Literally
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote:what deos it mean by one for the literal "xyz" i understand the new String("xyz") object but what is the 2nd one there ?

There are two string objects:

- The "xyz" is a String object, as a literal string in the source code
- Then you create another String object explicitly (by using new String(...)) that is initialized with the contents of the String object "xyz"

Note that because String objects are immutable, you should never write new String("xyz") - it's unnecessary to create a second String object that contains a copy of the literal string "xyz".
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . .
when you say new String("xyz") : . . . is it make sense?
Far better put than I would have
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic