Page 1 of 1

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

Posted: Mon Dec 28, 2015 11:23 pm
by elindsay
Couldn't o1 be a non-null reference to a primitive (as opposed to an object), though?

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

Posted: Tue Dec 29, 2015 3:41 am
by admin
Primitives do not have references.

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

Posted: Sat Mar 05, 2016 2:23 pm
by philcave
Couldn't the non-null reference be that of an interface?

e.g.

MyInterface o1 = null;

if (o1 instanceof Object)
System.out.println ("true");
else
System.out.println ("false");

This would print false.

So shouldn't the correct answer be option 4 - "For any non-null reference o1, the expression (o1 instanceof Object) may yield false." ?

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

Posted: Sat Mar 05, 2016 7:54 pm
by admin
philcave wrote:Couldn't the non-null reference be that of an interface?
Why do you think a reference to an interface will not be an Object?

e.g.

MyInterface o1 = null;

if (o1 instanceof Object)
System.out.println ("true");
else
System.out.println ("false");

This would print false.
Yes, it does print false. But not because o1 is not an instance of Object but because o1 is null.
Remember that every object in Java IS-A Object (because Object is the root class or all objects in Java). Therefore, o1 instanceof Object can never print false if o1 is not null.

HTH,
Paul.

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

Posted: Thu Mar 02, 2017 9:07 am
by AndaRO
You should understand here that instanceof operator returns true even if the Right Hand Side is a super class.

So it is the statement true for a "super super class"?

I think true because the inheritance relation is transitive.

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

Posted: Thu Mar 02, 2017 10:51 am
by admin
What happened when you tried it out?