• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String pool

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the following piece of code

...
...
String s1 = "Hello!";
String s2 = "Hello!";
s1 = null;
...

Totally how many objects are created and how many are eligible for garbage collection?
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1 and s2 both point to the same string literal. If s1 is set to null, there still exists s2 referring to the literal. So there should be none eligible.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 Object Gets Created
And
0 Objects Are eligiable For GC.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the String object created is it in the string pool or in the heap?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the String pool.

For eg:

String s = "Hello" -->Hello is dropped into the String pool and s will refer to it.

String s = new String("Hello") -->Hello is created in non-pool memory, and it is also placed in the pool.

In addition this might help you:
https://coderanch.com/t/252763/java-programmer-SCJP/certification/diffrence-heap-pool
[ May 03, 2006: Message edited by: Aniket Patil ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when u try to instantiate a string object, it will initially search for a object in the pool with same value. if no object is found then it will create one for u and returns that object reference, if there is some object already in the pool, it will retur the reference of the same object.
so for the code u mentioned there are two reference variables pointing to one object.as, some reference is pointing to the object, no object is free, so no garbage collection.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completely agree with Rushikesh Wagh
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic