About Question enthuware.ocajp.i.v8.2.883 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

About Question enthuware.ocajp.i.v8.2.883 :

Post by heleneshaikh »

Hello,

I have modified the question and was wondering if 'obj' is eligible for GC right after line 4? objArray[1] still points to obj, so I'm not sure. obj is now null, but still has a reference pointing to it and is therefore not eligible for GC. Am I correct?

Object obj = new String("a");
Object objArray [] = new Object[2];
objArray[1] = obj;
obj = null; //line 4

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.883 :

Post by admin »

Yes, you are correct. Even after line 4, objArray[1] is still pointing to the object pointed to by obj.

prasanna_ls
Posts: 7
Joined: Wed Nov 11, 2015 11:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.883 :

Post by prasanna_ls »

What if, instead of
objArr[0] = null;

at line 5, we had

objArr = null;

From what I understand, objArr would be eligible for GC. But not the String "aaaa" which is created at line 1. But when objArr gets GCd, the String "aaaa" will be eligible for GC. Or does the Garbage Collector have some special algorithm which enables it to detect such cases?(so that it can collect both objects at the same time)

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.883 :

Post by admin »

You may want to read about "island of isolation" here: http://stackoverflow.com/questions/7928 ... collection

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests