The instanceof Operator in Java

The instanceof operator compares an object to a given type and checks if the type of object in left side is assignment-compatible to the type in the right side. i.e., if we can say right-side-type = left-side-type object without any casting.

In Java, you can assign a child to a parent (or grand parent).  So we can exlpain instanceof in simpler terms as, instanceof operator compares an object reference (left side) to a type (right side) and see if the type of the actual object in the reference in left side is a child (or sub child) of the type in right side.


Example 1: Normal cases

package inheritance;

interface I {

class A implements I {

class B extends A {

class C extends B {

public class InstanceOfCheck {

	public static void main(String[] args) {

		A a = new A();
		B b = new B();
		C c = new C();
		A ac = new C();
		// C cc = ac is not possible,
		// but ac Instance of C will return true.

		System.out.println(a instanceof A);
		System.out.println(a instanceof I);

		System.out.println(b instanceof B);
		System.out.println(b instanceof A);
		System.out.println(b instanceof I);

		System.out.println(c instanceof C);
		System.out.println(c instanceof B);
		System.out.println(c instanceof A);
		System.out.println(c instanceof I);

		System.out.println(ac instanceof C);

Here, all the print statements will return true.


Additional Cases

  1. You cannot compare two incompatible types, which does not have any possibility of becoming compatible.

    1. You will get a compiler error with text as "Incompatible conditional operand types..." if compiler does not see any possibility of getting a true.

      • For instance, if A and B are two unrelated classes, and if we compare an object of A with instanceof to B, then compiler will give an error: Incompatible conditional operand types A and B. 

    2. If B is an unrelated interface to A, then there is a possibility that a child of A can also implement B and get assigned to a reference of A.

      • However in this case, B was a class, a child of A (which has already extended from A), will not be able to extend one more class.

      • Also, if we make the class A as final, then even if B is an interface, compiler will not allow to compile as A cannot be subclassed and there is no more probability of one of A's child implementing the interface.

  2. The null is not an instance of anything. So if we compare null to any type it will return false.


Example 2: Additional Cases


package inheritance;

interface I2 {

class A2 implements I2 {

class B2 extends A2 {

class C2 {

public class InstanceofCheck2 {

	public static void main(String[] args) {
		I2 i = new B2();
		A2 a = new A2();
		B2 b = new B2();
		C2 c = new C2();

		// Below line will compile as A2 and B2 are related,
		// But will return false as A2 is not a child of B2.
		System.out.println(a instanceof B2); //line 1

		// Below line will not compile as
		// C2 and A2 are not compatible
		System.out.println(c instanceof A2); //line 2

		// Below line will compile.
		// C2 and I2 are not related now,
		// But a child of C2 (which could be held in c)
		// Could implement I2 in future.
		System.out.println(c instanceof I2); //line 3


If we add final to class C2, then line 3 compilation will fail, as C2 cannot be subclassed and hence there is no possibility of a relationship through C2's child implementing I2.


Additional Important Points

  1. The class java.lang.Class has a method instance, that does the same.

  2. For any non-null reference obj, the expression (obj instanceof Object) will always yield true.

  3. Note that instanceof operator does not accept objects as the right hand side operand.


Tips for exam

Just draw the inheritance hierarchy diagram from the question, with a directed line from children to all its parents (draw only lines to immediate parents). If there is a direct or indirect path from the type of actual object on the left side to the type on the right side, then instance of will return true.



Nice explanation....... :)

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)