This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String constant pool or heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String constant pool or heap" Watch "String constant pool or heap" New topic
Author

String constant pool or heap

ilias basha
Ranch Hand

Joined: Nov 27, 2008
Posts: 55

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

Joined: Oct 13, 2005
Posts: 38087
    
  22
Which "xyz"? You have two there.
ilias basha
Ranch Hand

Joined: Nov 27, 2008
Posts: 55

sorry, I mean xyz in the following statement
new String("xyz")
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ilias basha
Ranch Hand

Joined: Nov 27, 2008
Posts: 55

thanks for both of you
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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 ?


Start Earning Online||Start Earning Using Java
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 12, 2001
Posts: 1179

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

Regards, Rene Larsen
Dropbox Invite
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Oct 13, 2005
Posts: 38087
    
  22
Seetharaman Venkatasamy wrote: . . .
when you say new String("xyz") : . . . is it make sense?
Far better put than I would have
 
 
subject: String constant pool or heap
 
Similar Threads
String pooling and garbage collector
Strings and their constant pool
String Object and GC
String Constant Pool - Help Needed
String -query