1An instanceof in Java is a comparison operator which, given an object instance,
2checks whether that instance is of a specified type (class/sub-class/interface)
3or not. Just like other comparison operators, it returns true or false.
4
5Comparing any object instance with a null type through instanceof operator
6returns a false.
7
8Instanceof operator is used to test the object is of which type.
9
10Syntax : <reference expression> instanceof <destination type>
11Instanceof returns true if reference expression is subtype of destination type.
12Instanceof returns false if reference expression is null.
1The instanceof operator tests to see if the prototype property of a constructor
2appears anywhere in the prototype chain of an object. The return value is a
3boolean value.
4For example :-
5
6function Car(make, model, year) {
7 this.make = make;
8 this.model = model;
9 this.year = year;
10}
11const auto = new Car('Honda', 'Accord', 1998);
12
13console.log(auto instanceof Car);
14// expected output: true
15
16console.log(auto instanceof Object);
17// expected output: true
18
1The instanceof keyword checks whether an object
2is an instance of a specific class or an interface.
3The instanceof keyword compares the
4instance with type. The return value
5is either true or false .
1if (cobj instanceof Child)
2 System.out.println("cobj is instance of Child");
3else
4 System.out.println("cobj is NOT instance of Child");
5
6// Source: https://www.geeksforgeeks.org/java-instanceof-and-its-applications/
7