8

I know that instanceof returns TRUE when an object is an instance of a particular class. For instance:

B extends A
C extends A

B b = new B();
C c = new C();

b instanceof A // returns TRUE

So far so good, so let's enter something that would seem like it should return false:

c instanceof B // won't compile (error: inconvertible types)

This doesn't compile, which makes sense because it allows an oversight to be caught at compile time. But, when DOES instanceof actually return false? It seems the only two options are TRUE and ERROR. The only exception I can think of is this:

null instanceof A // returns FALSE

But by the same logic as above, it would seem this should be caught at compile time as well.

What am I missing here? Are true / error the only practical options, or is it possible to actually return false in a more meaningful way, aside from when null is given as a reference variable?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
abc32112
  • 2,457
  • 9
  • 37
  • 53
  • That's safaris spell checker. Consider the above pseudo code. ;) – abc32112 Jan 22 '14 at 23:19
  • possible duplicate of [What is the 'instanceof' operator used for?](http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for) – Oliver Charlesworth Jan 22 '14 at 23:25
  • 1
    `(Object)c instanceof B` will compile. The reason `c instanceof B` doesn't compile is the compiler can determine statically that a reference to a `C` can never be a `B`. – Radiodef Jan 22 '14 at 23:44

4 Answers4

8

Here's an example that evaluates to false:

class A {}
class B extends A {}

A a = new A();
a instanceof B   // false

Live demo: http://ideone.com/cQltqE.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
5

In addition to the other answers: any time either the expression on the left, or the type on the right, is an interface, then instanceof could be true, so the compiler has to allow it. So it's easy to come up with examples where it would be false.

ajb
  • 31,309
  • 3
  • 58
  • 84
3

Here's another example that returns false

public static void main(String[] args) {
    checkType("");
}

static <T> void checkType(T sometype) {
    System.out.println(sometype instanceof A);
}

static class A {}

It's really only useful when trying to compare (and then cast) to a more specific type.

In your example,

c instanceof A

doesn't make sense since the type C is not in the inheritance hierarchy of A.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0
B extends A
C extends A

B b = new B();
C c = new C();

b instanceof A // returns TRUE
myMethod(b); // has TRUE and then FALSE
myMethod(c); // has FALSE and then TRUE

void myMethod(A a){
  if(a instanceof B){System.out.println("This is a B");}//first statement
  if(a instanceof C){System.out.println("This is a C");}//second statement
}

In call of myMethod(b), if evaluation in second statement of this method will be FALSE and will print This is a B. However in call of myMethod(c) if evaluation in first statement of this method will be FALSE and will print This is a C