File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

Hasitha Randika
Ranch Hand

Joined: Sep 04, 2007
Posts: 50
class A{
Short s = 5;
A go(A cb){
cb = null;
return cb;
}
public static void main(String ar[]){
A c1 =new A();
A c2 =new A();
A c3 =c1.go(c2);//line 1
c1 = null;
//do stuff how many objects are eligable for the GC
}
}

what will happen when line 1 execute
Ratnankur Kushary
Greenhorn

Joined: Sep 06, 2007
Posts: 6
Originally posted by Hasitha Randika:
class A{
Short s = 5;
A go(A cb){
cb = null;
return cb;
}
public static void main(String ar[]){
A c1 =new A();
A c2 =new A();
A c3 =c1.go(c2);//line 1
c1 = null;
//do stuff how many objects are eligable for the GC
}
}

what will happen when line 1 execute


Hi Hasitha,

Why dont you install eclipse in your PC and compile the program and try to interprete the error?? It will help you to understand type casting better than anything else. Try many more similar examples.

Thanks,
Ratnankur
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by Ratnankur Kushary:


Why dont you install eclipse in your PC and compile the program and try to interprete the error??


This code doesnt give any error, its about Garbage collection
when line 1 execute, you are setting reference variable 'c3' to null as a result of invoking go().
In your program only object 'c1' is eligible for GC
Ratnankur Kushary
Greenhorn

Joined: Sep 06, 2007
Posts: 6
Originally posted by Ahmed Yehia:

This code doesnt give any error, its about Garbage collection
when line 1 execute, you are setting reference variable 'c3' to null as a result of invoking go().
In your program only object 'c1' is eligible for GC


Hi Ahmed,

Actually the first line inside the class is :- "Short s = 5;" and not "short s = 5;" I was trying to suggest her that she should compile the program first and then copy the same piece of code here.

Thanks,
Ratnankur
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Both statemets are fine

If you have problems compiling the first statement then probably you are using older JDK, requires at least JDK 1.5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

Hasitha asked:
what will happen when line 1 execute

A c3 =c1.go(c2);
The method "go" of type A will be invoked through object c1 with object c2 as a parameter. The result (null) will be assigned to the new variable c3.
The method:
First line:
A go(A cb){
"cb" is a variable (not an object!) that refers to the same object (not a variable!) as c2 does. The method starts with this reference.
Second line:
cb = null;
The variable "cb" now points to null. This does not change anything with any of the objects, think about it.
From now on, the object that was refered by the parameter cb is no longer accessible in the method. But it is still there, as it is still refered by "c2" from the second line of main.

And in the third line, the null is returned.


Remember, you cannot null an object, you can only null a variable.


Ratnankur wrote:
Why dont you install eclipse in your PC and compile the program and try to interprete the error?

No.

Don't.

Don't use any IDE when preparing for SJCP.
Totally contraproductive.

And: Did Hasitha wrote anything about an error?



About the question:
Such a question would not occur on the exam, because of the Short.
Theses wrappers are in a wrapper pool and there will be no questions on that topic.

So perhaps try to solve this problem instead, it's the same as the one provided from Hasitha, only without wrappers:


The first object is eligible, the second is still referred by c2.
With the first object eligible, also the X-object (A has a X) is eligible, so totally two. This would be not the case if A had a Short.


Yours,
Bu.


all events occur in real time
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
Originally posted by Burkhard Hassel:


The first object is eligible, the second is still referred by c2.
With the first object eligible, also the X-object (A has a X) is eligible, so totally two. This would be not the case if A had a Short.


Yours,
Bu.



here when object c1 is eligible for garbage collection, the object x associated with object c1 will not be eligible for garbage collection, right?
because object X is still being referred by reference variable x. but yes, it won't be accessible since c1 object is now null.
So, ultimately only one object c1 will be eligible for garbage collection.
Correct me please if I'm going wrong.


[ September 28, 2007: Message edited by: Sarah Jorden ]
[ September 28, 2007: Message edited by: Sarah Jorden ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8803
    
    5
Someone *borrowed* a question from K&b and changed it a bit, oh well... in any case it should read Short s = 200; then you don't have to worry about the wrapper pool.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by Sarah Jorden:

here when object c1 is eligible for garbage collection, the object x associated with object c1 will not be eligible for garbage collection, right?

Total of two objects will be eligible for GC, object 'c1' but it has an associated unreferenced object which also become eligible, there is no way you can get to that object, it went out of scope. Think of it as locally declared object in a method, if the object isnt referenced from outside the method, it vanishs when the method returns.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

Sarah wrote:

here when object c1 is eligible for garbage collection, the object x associated with object c1 will not be eligible for garbage collection, right?
because object X is still being referred by reference variable x. but yes, it won't be accessible since c1 object is now null.
So, ultimately only one object c1 will be eligible for garbage collection.
Correct me please if I'm going wrong.


If you are in doubt, try this:



Yours,
Bu.
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
Hey Burkhard,

Thanks for the example. I got it now.

So whenever we want to know 'How many objects are eligible for Garbage Collection?' we can use System.gc() and finalize(), right?
or are there any cases when we can't actually know?
e.g. if somehow we end up having some object which is being referenced but we can't access it. Same as I thought about object X in this question.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

Sarah:
So whenever we want to know 'How many objects are eligible for Garbage Collection?' we can use System.gc() and finalize(), right?

Maybe, but you cannot control GC behaviour. Calling System.gc() might make the GC doing its job, but you cannot be sure.

and:
or are there any cases when we can't actually know?

Yes, if you have only a class file (not the source code) and the class is final.
Then we can't insert a finalize override nor extend the class.
Example: class String.

Another situation is on the actual exam. I'm in doubt if they let you check the questions with a compiler...

For solving these kind of GC questions it may also be wise to draw the situation on a piece of paper, like in this old thread:
http://www.coderanch.com/t/260221/java-programmer-SCJP/certification/Garbage-Collection-clarification


Yours,
Bu.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GC
 
Similar Threads
Garbage Collection
garbage collection?
gc eligible objects
help scjp5.0 question
Garbage Collection