• 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

Doubt on garbage collection?

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:-when the program is execute how many objects will be collected by the garbage collection process.




Ans-1
but , I get it why it is 1,but i want access that ungarbageble object and how to access it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the "test" constant, which you can access as that - "test".
That is also the object returned by s1.intern(), even though s1 is a completely different object.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
___________________________________________________________________________
public class Test1{public static void main(String[] args){String s="test";String s1 = new String("test"); System.out.println(intern());s=s1=null;}}

Ans-1
but , I get it why it is 1,but i want access that ungarbageble object and how to access it
__________________________________________________________________________

In my opinion ,in the above mentioned code you have created one object having two refrences.However you have also using intern,though calling intern() you need to specify the object either s or s1 ( in our case).
Let us suppose ,we called s.intern() . it means the "test" object would not be created and will only refreced to s1 from the private string pool of a class.( i belive you must have idea about string pool).
Assigning null to any refrence does not mean it will be garbage collected ,it just mean that " it eligible for garbage collection".

Hope it above justification would be helpful.
________________________
Mintoo
SCJP 1.4
__________________________
 
Ashok Pradhan
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mintoo,but I think the following program creates two objects:-



Because it returns false,so they refer to two different objects.

According to the following link, i can access that ungarbagable object by intern() method, can i access this object after nulling the two references.
web page
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search for String pool, that should tell you all you need to know.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic