About Question enthuware.ocajp.i.v7.2.920 :

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

Moderator: admin

georgeferreira
Posts: 2
Joined: Fri Jun 22, 2012 8:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by georgeferreira »

The question shows the follow code:

Code: Select all

public class NewClass{
   private Object o;
   void doSomething(Object s){  o = s;   }

   public static void main(String args[]){
      Object obj = new Object(); // 1
      NewClass tc = new NewClass(); //2
      tc.doSomething(obj); //3
      obj = new Object();    //4
      obj = null;    //5
      tc.doSomething(obj); //6
   }
}
At line 5 the seconde Object object lost all references, since question says "Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?" the correct options shoud be line 5 or is GC only start on line 6 on Object?

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

At //5 only the local variable obj is set to null but the original object created at //1 is still being referred to by the instance variable o of tc (Notice the call tc.doSomething(obj); which sets tc.o to point to the same object). This instance variable is changed to point to another object at //6. So it is only after //6 that the original object created at //1 is not being pointed to by any reference.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javabeats

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by javabeats »

I think Line 6 is correct, because only after obj (which is now 'null') is passed to the doSomething() method, the original Object created on Line 1 will have no reference at all.

ksnortum
Posts: 30
Joined: Fri Dec 07, 2012 6:09 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by ksnortum »

The explanation in the exam is unclear; the one given here by the admin is much better.

zkalev
Posts: 2
Joined: Fri Jul 26, 2013 2:44 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by zkalev »

Code: Select all

public class NewClass{
   private Object o;
   void doSomething(Object s){  o = s;   }

   public static void main(String args[]){
      Object obj = new Object(); // 1
      NewClass tc = new NewClass(); //2
      tc.doSomething(obj); //3
      obj = new Object();    //4
      obj = null;    //5
      tc.doSomething(obj); //6
   } 
}
In line //4 new Object is created and only "obj" pointing to this new object.In line //5 obj starts to point to null.Then isn't the new object from line //4 ready to be garbage collected.It's should be the same like this:

Code: Select all

  obj = new Object();    //4                                        Object obj = new Object(); 
      obj = null;    //5                         =>                                    obj = null;   

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

zkalev wrote: In line //4 new Object is created and only "obj" pointing to this new object.In line //5 obj starts to point to null.Then isn't the new object from line //4 ready to be garbage collected.
Yes, that is correct.
zkalev wrote: It's should be the same like this:

Code: Select all

  obj = new Object();    //4                                        Object obj = new Object(); 
      obj = null;    //5                         =>                                    obj = null;   
I am not sure I understand what you mean by this.

-Paul.
If you like our products and services, please help us by posting your review here.

zkalev
Posts: 2
Joined: Fri Jul 26, 2013 2:44 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by zkalev »

I mean that I think line //5 is correct answer for the question....


P.S
But now I realize that I missed up that part from the question: object created on line // 1

Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?

Sorry I should be more careful!

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by javanaut »

Could the object reference at line 5 be set to a new Object and the object originally created at line 1 still be eligible for garbage collection? So does it make a difference that line 5 is set to null? Does this just make the code and the instructions a little bit more confusing for the test taker? :lol:

:?

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

javanaut wrote:Could the object reference at line 5 be set to a new Object and the object originally created at line 1 still be eligible for garbage collection? So does it make a difference that line 5 is set to null? Does this just make the code and the instructions a little bit more confusing for the test taker?

:?
Line 5 makes no difference. obj starts pointing to something else at Line 4 itself. It is there just to cause confusion :)

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by javanaut »

:D

Got it. Thank-you for the reply admin Paul. I thought this was the case, that the object instance variable with the private access modifier in the other class could be set to point to some other object not necessarily set to null to make it eligible for garbage collection. :D

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by fasty23 »

Is it correct: As obj starts pointing to something else at Line 4 that reference of obj from line 1 is eligible for gb at line 4? or because of "assuming no compiler optimizations are done" it not happen?
If we don't assuming no compiler optimizations are done, which line was true?
tnx

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

Compiler optimizations can cause the compiler to not generate any code if it can figure out that the code is redundant. So it is not possible to predict. For the purpose of the exam, always assume that there are no compiler optimizations being performed.
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by jbilkes »

hi all

i choose line 4. Why? because i was thinking that when passing the object as argument to the doSomething, it is copied. And since the question is whether the actual original object would be eligible i thought after line 4 since there is only 1 unique (!) instance (by the way per definition the case for an object as far as i know) and that object is referenced to by obj and not by o (this is the copied version as believed). Since obj gets referenced to a new instance at line 4 this line is far from arbitrary IMHO

would like to hear your informed thought on this admin Paul

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

jbilkes wrote:hi all

i choose line 4. Why? because i was thinking that when passing the object as argument to the doSomething, it is copied.
There is fundamental flaw in your understanding of objects and references. Object is never copied while passing. References are.
Further, references are never garbage collected. Objects are.

Please go through the following links to clear up your understand and then this question will look really easy to you.
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.yoda.arachsys.com/java/passing.html



HTH,
Paul.
If you like our products and services, please help us by posting your review here.

anief786
Posts: 1
Joined: Wed Sep 02, 2015 12:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by anief786 »

At Line 1: obj -> new object() @1234
At Line 2: o -> null;
At Line 3: tc.doSomething(obj); i.e., o, obj -> new object @1234
At Line 4: obj -> new object() @5678 , o-> new object @1234
At Line 5: obj -> null, new object() @5678 is eligible for garbage collection.

Line 5 would be the right answer. Could anyone clarify if i am anything wrong here, Thanks in advance?

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

>At Line 5: obj -> null, new object() @5678 is eligible for garbage collection.

The question is asking about the object created at //1 i.e. new object() @1234.
At //5, o is still pointing to 1234.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by heleneshaikh »

I do not understand why the object created on line 1 is not eligible for GC immediately after line 4.
obj = new Object(); // obj now points to a new Object and no longer refers to the Object created on line 1. Hence, the object created on line 1 has no references pointing to it at this point and -in my understanding- should therefore be eligible for GC.

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

Because reference o is still pointing to that object.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by Mihai1977 »

Hi,


Line 1: object "no1" is created, "obj" points to object "no1";
Line 2: object "NewClass" is created, "tc" points to object "NewClass";
Line 3: reference "o" point also to Object "no1";
Line 4: object "no2" is created, "obj" now points to "no2", "s" still points to object "no1";
Line 5: "obj" points to null, so "no2" is ready for garbage collector, "s" still points to object "no1";
Line 6: a new reference "o" points again to object "no1"???

Please explain me if I understood correct, thx.

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

Mihai1977 wrote:Hi,


Line 1: object "no1" is created, "obj" points to object "no1";
Line 2: object "NewClass" is created, "tc" points to object "NewClass";
Line 3: reference "o" point also to Object "no1";
Correct.
Line 4: object "no2" is created, "obj" now points to "no2", "s" still points to object "no1";
s exists only inside the method doSomething. Once the method returns there is no "s" anymore at line 4.
But o, which is a instance member still points to object no1.
Line 5: "obj" points to null, so "no2" is ready for garbage collector, "s" still points to object "no1";
Again, there is no s at this point. But o, which is a instance member still points to object no1.
Line 6: a new reference "o" points again to object "no1"???
o is an instance member. So there is only one o per NewClass object. Since doSomething is being called on the same NewClass object, the same o will now be changed to point to null (because null is being passed to doSomething).

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by Mihai1977 »

Thx Paul, now it's all clear!

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by flex567 »

I don't understand,
In line 5 no reference variable is pointing to the object created in line 1??
Why is the answer line 6?
Is it because parameter reference variable "s" is still pointing to the object?

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

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by admin »

The instance variable o of NewClass object still has as reference to the object created at line 1.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by flex567 »

Aha, this one is pretty simple, but I got confused while reading so many comments.
Maybe some of the comments can be deleted.

morarumaria1988
Posts: 5
Joined: Mon Oct 30, 2017 5:17 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.920 :

Post by morarumaria1988 »

admin wrote:
Fri Jun 22, 2012 10:43 am
At //5 only the local variable obj is set to null but the original object created at //1 is still being referred to by the instance variable o of tc (Notice the call tc.doSomething(obj); which sets tc.o to point to the same object). This instance variable is changed to point to another object at //6. So it is only after //6 that the original object created at //1 is not being pointed to by any reference.

HTH,
Paul.
What is here the local variable obj? The one created at line //4 ?

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests