• 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

GC

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
https://coderanch.com/t/260221/java-programmer-SCJP/certification/Garbage-Collection-clarification


Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic