jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Doubt on garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt on garbage collection?" Watch "Doubt on garbage collection?" New topic
Author

Doubt on garbage collection?

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
___________________________________________________________________________
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

Joined: Dec 17, 2007
Posts: 180

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

Joined: Oct 27, 2005
Posts: 19653
    
  18

Search for String pool, that should tell you all you need to know.
 
Consider Paul's rocket mass heater.
 
subject: Doubt on garbage collection?
 
Similar Threads
Shifting and Garbage Collection
Quick == question
Reg. GC from JavaCaps
Explain the ouput
doubt in garbage collection