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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Question in GC" Watch "Another Question in GC" New topic
Author

Another Question in GC

Alan Hermin
Ranch Hand

Joined: Feb 16, 2006
Posts: 290
Hi ranchers....
i would to know if the object created in the pool is elegible for GC();
for example:
String str="hellow";
Or
Integer i1=5;


Like these: create that in the pool, but is elegible for gc?..
THANKs

[ April 30, 2006: Message edited by: alaa hassan ]
[ April 30, 2006: Message edited by: alaa hassan ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

String str="hellow"; creates an object on the heap. If you say str=null it will be available for garbage collection.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Alan Hermin
Ranch Hand

Joined: Feb 16, 2006
Posts: 290
OK....But what about the things created in the pool.........
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

The object is in the pool when you say String str = "hellow"; using the new keyword will create two objects.
Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
I wanted to confirm my understandings
1. String str="hello";
This will create only one string object
str-->[hello]

2. String str= new String("hello");

This will create 2 string object
unidentified reference-->[hello]
str-->[hello].

Please correct me if I am wrong.
Krishna Bulusu
Ranch Hand

Joined: Jan 27, 2006
Posts: 32
hi,
I think Integer i=5 is wrong here.
Jegon Jeganathan Renganathan
Greenhorn

Joined: Apr 21, 2006
Posts: 6
Hi Hasaan,

A good question. As mentioned by one of our friend,
String s= "hello"; //will create only one object in pool
String s = new String("hello");//will create two objects
As far as we are using the first line(which most of us are using) for creating string object, it will be stored in Magical (String pool) which is not eligible for GC.
But, as the second statement which is created in the Heap will be eligible for GC. Hope this will be clear for you.


When Going gets Tougher, the Tough gets Going
Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
Thanks,
void test()
{
String str="hello";
String str= new String("hello");
//line 4
}//line 5


After line 5 (i.e. when the function ends), how many objects will be eligible for gargabe collection? I think it will be 2 objects. Since as you mentioned the String str="hello" will not be stored in the heap but will be stored in string pool.
gurpreet singh
Ranch Hand

Joined: Apr 07, 2006
Posts: 38
So u mean
String s1 = new String("hello");

creates two objects

where are these 2 objects created?


SCJP
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Here is a good post about string & GC.
 
jQuery in Action, 2nd edition
 
subject: Another Question in GC