Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InstanceOf Doubt.

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: http://www.go4java.20m.com/mock1.htm
Here the answer is null,false,false.But null IS-A Object, then why is the answer false?

1 class AA{}
2 class BB extends AA{}
3 class Q6
4 {
5 public static void main(String arg[])
6 {
7 AA a=null;
8 BB b=(BB)a;
9 System.out.println(b);
10 System.out.println(b instanceof BB);
11 System.out.println(b instanceof AA);
12 }
13 }
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null instanceof any type will always result in false. You can read the Java Language Specification for that...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
But null IS-A Object...

No, null is not an object in Java. Where did you hear or read that null is an object?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic