Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does instanceof return FALSE?

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?

like image 234
abc32112 Avatar asked Jan 22 '14 23:01

abc32112


People also ask

What is the return type of the Instanceof operator?

The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

What does the Instanceof operator return in Java?

Java instanceof Operator The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

Does Instanceof check for NULL?

Using the instanceof Operator When an Object Is null If we use the instanceof operator on any object that's null, it returns false. We also don't need a null check when using an instanceof operator.


3 Answers

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.

like image 141
Oliver Charlesworth Avatar answered Oct 05 '22 16:10

Oliver Charlesworth


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.

like image 29
ajb Avatar answered Oct 05 '22 16:10

ajb


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.

like image 42
Sotirios Delimanolis Avatar answered Oct 05 '22 17:10

Sotirios Delimanolis