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

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

Moderator: admin

Post Reply
Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

In below example

TestClass t1, t2, t3, t4;
t1 = t2 = new TestClass();
t3 = new TestClass();

why can't t2 be an Object? Is this becuase t2 is assigned to t1 & hence after assignment it's not pointing to TestClass object?

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

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

Post by admin »

I am not sure I understand your confusion. t2 is declared of class TestClass, so you cannot assign new Object() to t2. The t1 = t2 part has nothing to do with it.
If you like our products and services, please help us by posting your review here.

Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

admin wrote:I am not sure I understand your confusion. t2 is declared of class TestClass, so you cannot assign new Object() to t2. The t1 = t2 part has nothing to do with it.

I want to know why t2 is not an object of class TestClass as the answer says? Also i am not sure what you mean by "t2 is declared of class TestClass, so you cannot assign new Object() to t2" as we are able to assign new Object() to t1 which is also declared of class TestClass.

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

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

Post by admin »

Where do you see new Object() being assigned to t1?

The question asks you how many objects and references are being created in the given code. Whenever you do a "new" of any class, you create a new object, so in this case there are only two objects being created. Both the objects are objects of class TestClass.

t1, t2, t3, and t4 are not objects. They are references that point to objects. In this case, t4 is not pointing to any object.
If you like our products and services, please help us by posting your review here.

Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

admin wrote:Where do you see new Object() being assigned to t1?

The question asks you how many objects and references are being created in the given code. Whenever you do a "new" of any class, you create a new object, so in this case there are only two objects being created. Both the objects are objects of class TestClass.

t1, t2, t3, and t4 are not objects. They are references that point to objects. In this case, t4 is not pointing to any object.

Thanks a lot for this clarification. I got it clear now.

mhasse
Posts: 1
Joined: Sun Jan 24, 2016 5:13 am
Contact:

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

Post by mhasse »

hi all

for


Given that TestClass is a class, how many objects and reference variables are created by the following code? TestClass t1, t2, t3, t4; t1 = t2 = new TestClass(); t3 = new TestClass();

why when i print

System.out.println(t1 instanceof Object);
System.out.println(t2 instanceof Object);
System.out.println(t3 instanceof Object);

does it print true for all? therefore i presume 3 object created?

tsk

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

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

Post by admin »

mhasse, you have asked a very fundamental question. To answer this question, you need to understand the difference an object and a reference. Please go through this first:
http://www.hacktrix.com/difference-betw ... ce-in-java
and
http://www.dreamincode.net/forums/topic ... reference/

After going through the above links, you need to read about the instanceof operator. This link is good: http://stackoverflow.com/questions/7526 ... of-in-java

Once you go through the above, it will be clear to you why all three lines print true but there are not 3 objects.

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

kirankumar
Posts: 5
Joined: Tue Apr 05, 2016 10:25 am
Contact:

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

Post by kirankumar »

System.out.println(t1); //enthuwaremocktest.test2.Ex67@3dccbdf7
System.out.println(t2); // enthuwaremocktest.test2.Ex67@3dccbdf7
System.out.println(t3); //enthuwaremocktest.test2.Ex67@4ccbc2d3


See above , only 2 objects are created
1) 3dccbdf7 (t1 and t2 referring same object)
2) 4ccbc2d3
and 3 references t.e., t1, t2, and t3.

Please let me know If I am wrong.

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

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

Post by admin »

You are right.
If you like our products and services, please help us by posting your review here.

aguywholikesjava
Posts: 1
Joined: Sun Aug 07, 2016 3:51 am
Contact:

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

Post by aguywholikesjava »

4 reference variables (t1, t2, t3, t4)
3 references (t1, t2, t3)
2 objects.

nah?

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

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

Post by admin »

Not sure what are you asking.
Paul.
If you like our products and services, please help us by posting your review here.

pupmonster
Posts: 1
Joined: Thu Jul 20, 2017 11:32 am
Contact:

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

Post by pupmonster »

>>>> See above , only 2 objects are created
>>>> 1) 3dccbdf7 (t1 and t2 referring same object)
>>>> 2) 4ccbc2d3
>>>> and 3 references t.e., t1, t2, and t3.

>> You are right.

Could the answer possibly be corrected in the next version?

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

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

Post by admin »

Ok, I see the confusion now.

No distinction is made between a reference and reference variable in Java because unlike c/c++, which has the concept of pointers as well as references, Java has only one thing - references. Each reference variable in Java stores a value - either that value is the address to an object or that value represents null.

For this reason, the term reference and reference variable are interchangeable in Java.

Therefore, in this case, there are 4 references (not three). t4 is not pointing to the same object as t1, t2, and t3 are pointing to, but t4 is a reference nonetheless, pointing to null.
If you like our products and services, please help us by posting your review here.

igor.simecki
Posts: 5
Joined: Tue Feb 06, 2018 2:41 am
Contact:

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

Post by igor.simecki »

admin wrote:Ok, I see the confusion now.

No distinction is made between a reference and reference variable in Java because unlike c/c++, which has the concept of pointers as well as references, Java has only one thing - references. Each reference variable in Java stores a value - either that value is the address to an object or that value represents null.

For this reason, the term reference and reference variable are interchangeable in Java.

Therefore, in this case, there are 4 references (not three). t4 is not pointing to the same object as t1, t2, and t3 are pointing to, but t4 is a reference nonetheless, pointing to null.
except reference variables in methods, in which case t4 doesn't store anything and will not compile if used.

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

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

Post by admin »

igor.simecki wrote:
admin wrote:Ok, I see the confusion now.

No distinction is made between a reference and reference variable in Java because unlike c/c++, which has the concept of pointers as well as references, Java has only one thing - references. Each reference variable in Java stores a value - either that value is the address to an object or that value represents null.

For this reason, the term reference and reference variable are interchangeable in Java.

Therefore, in this case, there are 4 references (not three). t4 is not pointing to the same object as t1, t2, and t3 are pointing to, but t4 is a reference nonetheless, pointing to null.
except reference variables in methods, in which case t4 doesn't store anything and will not compile if used.
Not sure what you mean. t4 is a reference variable. No matter whether it is used or not or where is it used. Can you post some code to show what you mean exactly?
If you like our products and services, please help us by posting your review here.

igor.simecki
Posts: 5
Joined: Tue Feb 06, 2018 2:41 am
Contact:

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

Post by igor.simecki »

void test(){
Object o1,o2;
o1= new Object();
o2.toString();
}

this will cause a compiler error because o2 references nothing.

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

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

Post by admin »

igor.simecki wrote:void test(){
Object o1,o2;
o1= new Object();
o2.toString();
}

this will cause a compiler error because o2 references nothing.
Did you read the error message that the compiler generated when you tried to compile the above code? The error is not because o2 points to nothing. The error is because you are trying to use o2 without initializing it. Try o2 = null; first and then see what happens.

Regardless, o2 is still a valid reference variable.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests