Jan tenpas

Greenhorn
+ Follow
since Nov 14, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jan tenpas

Oh nice, I think I have it now. Would the following addition to the code would break the reference to the b1 object and free it up for GC? (

1. class Beta {}
2. class Alpha {
3. static Beta b1;
4. Beta b2;
5. }
6. public class Tester {
7. public static void main(String [] args){
8. Beta b1 = new Beta(); Beta b2 = new Beta();
9. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
10. a1.b1 = b1;
11. a1.b2 = b1;
12. a2.b1 = b2; //CHANGE STATIC REFERENCE TO THE b2 OBJECT. WILL THIS FREE UP b1 FOR GC?!?
13. a2.b2 = b2;
14. a1 = null; b1 = null; b2 = null;
15. //do stuff
16. }
17. }
Good catch, I did put an extra blank line in the code at line 16.

The response from the books is as follows:

"It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta object through the static variable a2.b1--because it's static."

I guess what I'm not getting is why the b1 object in Tester is not eligible for garbage collection. Since a1 is set to null and b1 is set to null, then what is still referencing b1?

Thanks a bunch, it's starting to click!
I didn't see the response on page 246. Can you post the quote that references integer caching on page 246? Maybe I just have an older version or something...

Either way, looks like Henry nailed. Thanks!

I'm working in the McGraw Hill SCJP study guide for Java 6. They showed that the code below has only one object eligible for garbage collection. Can anyone explain which one and why?

Thanks in advance!!!



When line 16 is reached, how many objects will be eligible for garbage collection?

Answer: 1
I'm currently studying for the SCJP exam (or OCP JSEP now maybe?) and am having issues with the objective that deals with Wrappers/Boxing. I understand that Java sees Wrapper Objects as two separate objects and also as meaningful equal.

My question is why does it consider the Objects not equal if the value is large, but equal if the value is small? Shouldn't it see both as not equal since they are different objects? See below for the code I used to test (this was done in TextPad).

Integer l4 = 3;
Integer l5 = 3;

Integer i3 = 1000;
Integer i4 = 1000;

void longtest()
{
if(l4 != l5) System.out.println("different objects @ 3"); //no output, makes sense

if(i3 != i4) System.out.println("different objects @ 1000"); //output?!?
}