4

I'm studying 'instanceof' java, but I couldn't understand 'instanceof' clearly, I thought below answer would be true and false, but result is both true. Could you explain why this result happen? As I know, when A is child of B (Parent), and a instanceof B is 'false' but result is different with what I thought.

class Car{
    String color;
    int door;       
}

class FireEngine extends Car{
    void water(){
        System.out.println("water");
    }
}

public class Operator {
    public static void main(String[] args) {
        Car car = new FireEngine();
        FireEngine fireCar = new FireEngine();

        System.out.println(car instanceof FireEngine);
        System.out.println(fireCar instanceof Car);
    }
}
Changil Lee
  • 157
  • 1
  • 1
  • 6
  • 1
    You need to understand the difference between the type of a variable and the type of an object. Variable `car` has type `Car`, but its value is type `FireEngine`. Imagine that the variable is a bottle, a bottle can hold liquids. Beer is a liquid therefore you can put it in a bottle. If you do so, `bottle instanceof Beer` will return true, even if the bottle isn't labelled as beer bottle. – biziclop Apr 28 '15 at 09:57
  • 1
    A FireEngine is a car and a FireEngine is a FireEngine. It's about the object instantiated, not about how the variable is defined. – Jeroen Vannevel Apr 28 '15 at 09:57
  • 3
    Just my two cents: in real code, when using `instanceof`, it is 95% of the times a [code smell](http://en.wikipedia.org/wiki/Code_smell). It is 100% fine to use it for learning purposes and to understand typing and polymorphism, but in the future, when you actually write code, and have to use `instanceof` - it should raise a red flag that indicates "something in here is probably poorly designed". – amit Apr 28 '15 at 09:59

5 Answers5

7

Declaration != Value

You declare car as an Car, but the Value is an FireEngine.

instanceof works based on values, not on the declarations of their variables!!!

Shortening may help to understand:

System.out.println(new FireEngine() instanceof FireEngine);  // true
System.out.println(new FireEngine() instanceof Car);         // true
Grim
  • 1,938
  • 10
  • 56
  • 123
2

The output of instanceof depends on the runtime type of the variable whose type you are testing. The compile time type of the variable doesn't matter (as long as it is possible that x instanceof Y will return true for some value of x, otherwise the expression won't pass compilation).

Both car and fireCar hold instances of FireEngine in your code. And since FireEngine is a kind of a Car, both car and fireCar are instanceof both Car and FireEngine, so your code prints true and true.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Implementation of the Instanceof operator. Returns a Boolean if the Object parameter (which can be an expression) is an instance of a class type.

Input 1: An object or Expression returning an object.

Input 2: A Class or an Expression returning a Class Returns: A Boolean that is the result of testing the object against the Class.

For more information please go throught the javadocs @ http://docs.oracle.com/cd/E13155_01/wlp/docs103/javadoc/com/bea/p13n/expression/operator/Instanceof.html

For more detailed explanation with examples please go through the following web page : http://mindprod.com/jgloss/instanceof.html

Community
  • 1
  • 1
Sumit Surana
  • 1,554
  • 2
  • 14
  • 28
1

In Java there are two types of bindings: static (the reference type) and dynamic (the object type).

In your case:

 Car car = new FireEngine();

Car is the static type and FireEngine is dynamic type. It means, you are actually working with a FireEngine (the object type). You can imagine Car as a special pointer with a car shape pointing to the real object that is your awesome FireEngine. If you read 'instanceof' you can understand it, this method tell you if an object is an instance of a class, not the reference. So the compiler will see: FireEngine (car) instanceOf FireEngine? Of course, let's return a true!

You can have a look to this post also: What is the 'instanceof' operator used for?

Community
  • 1
  • 1
Esteban S
  • 1,859
  • 5
  • 22
  • 43
1

The statement

As I know, when A is child of B (Parent), 
and a instanceof B is 'false' but result is different with what I thought.

is not correct. instanceof does not check for the child, it tests for the parent.

Grim
  • 1,938
  • 10
  • 56
  • 123