Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SCJP question:how many objects are eligible for the garbage collector

Posted on 2011-04-23
7
Medium Priority
?
1,794 Views
Last Modified: 2012-06-21
   interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println("whinny"); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println("vinny"); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
12. Icelandic i3 = new Icelandic();
13. i3 = i1; i1 = i2; i2 = null; i3 = i1;
14. }
15. }
When line 14 is reached, how many objects are eligible for the garbage collector?
I dint understand the concept of how to calculate the objects that are eligible for the garbage collector?can somebody explain it??

Open in new window

0
Comment
Question by:chaitu chaitu
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 35455321


This is a good experiment - small program
which shows that indeed both i1 and i3 point at second object
at line 14:


public class GCCheck {
}
interface Animal { void makeNoise(); }
class Horse implements Animal {
Long weight = 1200L;
public void makeNoise() { System.out.println("whinny"); }
}
 class Icelandic extends Horse {
     int i;
 public void makeNoise() { System.out.println("vinny"); }
 public static void main(String[] args) {
 Icelandic i1 = new Icelandic(1);
 Icelandic i2 = new Icelandic(2);
 Icelandic i3 = new Icelandic(3);
 i3 = i1; i1 = i2; i2 = null; i3 = i1;
     System.out.println("i1 " + i1);
       System.out.println("i2 " + i2);
       System.out.println("i3  " + i3);

 }
 Icelandic(int i){
   this.i = i;

 }

public String toString() {
    return (i + "");
}

 }

Open in new window


Output:

i1 2
i2 null
i3  2

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35455329
When line 14 is reached, how many objects are eligible for the garbage collector?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35455341
four are eliegible becuae
your previous Iceladic objects one and object three and each of them had
alos long value inside which can be GC'ed when the enclosing object disappears,
- so the total will be four which are eligible
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 35455347
As quoted from my link, it should be 4.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35455349
Icelandic originally pointed by i1 i1 and long weight inside it are eligivble for GC
Iclendic  originally pointed by i3 and long weight inside it are eligible for GC

So the total will be 4 objects are eligible
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question