aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Islands of isolation 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 "Islands of isolation" Watch "Islands of isolation" New topic
Author

Islands of isolation

babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }


Can anyone explain me the islands of isolation topic, I found it very confusing?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

as per polya, if you draw a picture, it becomes clearer.



at this point, you have three references (e1, e2 and e3), each pointing to a unique object in the heap.



e1.e = e3;

e2 = null;
e3 = null;

e2.e = el;

e1 = null;

so at this point, e1, e2, and e3 all point to nothing. obj1 has a reference to obj3. that has a reference to obj2, which has a reference to obj1. Every object is referred to by SOMETHING, so they're not eligible for GC... However, you have no way to GET to them. They're all alone, each referring to each other in a little island in the heap. They're isolated from the rest of the code/references. Therefore, since you can't GET to them, even though they all HAVE a reference, they CAN be GC'd.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Here all the three objects are referencing to each other through their 'e' variable.However as you have nullified them.Now, you cannot access them in any manner, but still the three objects are lying somewhere on your computer's ram.
These are called 'islands' as they are isolated from the user (you) but still have got references(amongst themselves).
This is quite tricky and you should expect questions that ask about how many objects are eligible for garbage collection(as of java 5 exam) at a given line of code.
Hope this helps
babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
Thanks buddies.
varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
e2 = null;
e3 = null;

code:


e1 -> obj1---
e2 obj2 |
^ |
| |
e3 obj3<--


e2.e = el;


how can we say this as e2 = null and how can we access e2.e it is same as saying null.e=e1;


Beat the world,if you can.......
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

varinder mahajan is right. Statement (1) will throw a NullPointerException.

1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el; //(1)
11. e1 = null;
12. }
13. Eco e;
14. }

also at (1) e2.e = el; must be e2.e = e1;
I think the whole statement (1) is wrong....Please babul bansal check your code...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
Originally posted by babul bansal:
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }

Can anyone explain me the islands of isolation topic, I found it very confusing?



I think this code trying to confuse you about Island Of Isolation, but the "real" problem is the line #10, which throws NullPointerException. You will face some questions like this in the exam.
But anyway, it's good to be good at Island of Isolation.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
Well I check the code, its a question given in the Kathy Sierra book under Self Test-12th question.

I am again posting the question with options and answer.
Given:

1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }

At what point is only a single object eligible for GC?

a)After line 8 runs.

b)After line 9 runs.

c)After line 10 runs.

d)After line 11 runs.

e)Compilation fails.

f)Never in this program.

g)An exception is thrown at runtime

ANSWER:
G is correct. An error at line 10 causes a NullPointerException to be thrown because e2 was set to null in line 8, If line 10 was moved between lines 7 and 8, then F would be correct, because until the last reference is nulled none of the objects is eligible, and once the last reference is nulled, all three are eligible.

Prashanth Patha
Ranch Hand

Joined: May 06, 2011
Posts: 62

Thanks for all... this entire discussion lead me to have good knowledge on island of isolation..


Thanks & Regards
Prashanth

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Islands of isolation