aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is Stringpool and heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is Stringpool and heap" Watch "what is Stringpool and heap" New topic
Author

what is Stringpool and heap

Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
hi friends,

what is stringpool and what is heap

Thanks in advance.
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
String pool itself a heap. String pool is like a bucket of string constants.


Manivannan
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
mani can u please ellaborate...
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
String pool is like a bucket which has string constants.

For instance,
Object a = new Object();
Object b = new Object();
After execution of the above statements, two objects are created and placed on heap.

But,
String s1 = "abc";
String s2 = "abc";
After these two statements, only 1 object is created (on string pool).
Since, they are string constant(objects).

Now,
String s1 = new String("abc");

After above statement is executed, there created two objects.
One object is created on heap (and referenced by 's1').
Another object is created on String pool.

The difference is:
The object created on heap are garabage collectible (when they are eligible).
But, The object created on string pool are never garbage collected.
[ November 16, 2005: Message edited by: Mani vannan ]
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

stringpool - pool of unique string contants or string literals. it will be used by JVM to reuse when same string literal comes.

heap - where objects and member variables are get stored.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi...

String s1 = "abc";
String s2 = new String("abc");
String s3=new String("abc");

How many objects would be there in the string pool....

In heap... i guess it is 2...created by new

Regards
[ November 16, 2005: Message edited by: A Kumar ]
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
Totally 3 objects. 1 in string pool, 2 in heap. is that right kumar?
achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
hai mani,
u said objects created in the pool are never garbage collected.But what i think is "they will be eligible for garbage collection when jvm unloads the class".What do u say about this?
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
is that right


Even i feel that it is the answer but not quite sure....
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

String s1 = "abc";
String s2 = new String("abc");
String s3=new String("abc");

How many objects would be there in the string pool....

One.

The String pool is a collection of String objects that have been interned. String literals are automatically interned. So, when the class containing this code is loaded, a String object will be created containing the character sequence ['a','b','c'], and it will be interned. All occurrences of the literal "abc" in this code will refer to the same String object.

With the following lines, two new Strings are created and initialized with the same character sequence. These strings are not pooled.

There will be three distinct String objects created by this code, but only one of which will be pooled.

Hope this helps!
[ November 16, 2005: Message edited by: Steve Morrow ]
RAMA KRISHNA AALLA
Greenhorn

Joined: Aug 10, 2005
Posts: 29
Hi! Ranchers

String s1 = "abc";

will s1 be eligible for Garbage Collection or not?

Thanks in advance,
Rama Krishna Chowdary
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
Hi,

By the code,



A String object will get created in the heap and reference will be put into the String Pool. By that statement I mean to say is "Programmer wont have any control over that reference." So, whatever the program logic is, "abc" will be pointed by String Pool.

And thats y it wont be garbage collected....

Hope this will help...tell me if I'm goin wrong.

Regards,
Mausam
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Thank you for giving feedback.

Regards
krishna
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

will s1 be eligible for Garbage Collection or not?

s1 is a reference variable, not an object; only objects are eligible for garbage collection. In this case, there is a single String object, referred to both by the literal and by the variable s1. Because a strong reference is held to the String object, it is not eligible for garbage collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is Stringpool and heap