• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Question in GC

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str="hellow"; creates an object on the heap. If you say str=null it will be available for garbage collection.
 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK....But what about the things created in the pool.........
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object is in the pool when you say String str = "hellow"; using the new keyword will create two objects.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I think Integer i=5 is wrong here.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nilesh Deshpande
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So u mean
String s1 = new String("hello");

creates two objects

where are these 2 objects created?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a good post about string & GC.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic