About Question enthuware.ocajp.i.v7.2.1177 :
Moderator: admin
-
- Posts: 1
- Joined: Mon Dec 28, 2015 11:15 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1177 :
Couldn't o1 be a non-null reference to a primitive (as opposed to an object), though?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1177 :
Primitives do not have references.
-
- Posts: 4
- Joined: Sat Mar 05, 2016 2:15 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1177 :
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." ?
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." ?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1177 :
Why do you think a reference to an interface will not be an Object?philcave wrote:Couldn't the non-null reference be that of an interface?
Yes, it does print false. But not because o1 is not an instance of Object but because o1 is null.
e.g.
MyInterface o1 = null;
if (o1 instanceof Object)
System.out.println ("true");
else
System.out.println ("false");
This would print false.
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.
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1177 :
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.
So it is the statement true for a "super super class"?
I think true because the inheritance relation is transitive.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1177 :
What happened when you tried it out?
Who is online
Users browsing this forum: Bing [Bot] and 5 guests