What are legal operands of instanceof operator

What are legal operands of instanceof operator ?

Answer : The instanceof operator and its general form:

objref instanceof type

where 

  • Left operand (objref) is an an object reference
  • Right operand (type) can be be a class or an interface

Please see example below:

package com.javahonk.operatortest;

public class InstanceOfTest {

    public static void main(String[] args) {
	A a = new A();
	B b = new B();
	C c = new C();
	D d = new D();

	if (a instanceof A)
	    System.out.println("a is instance of A");
	if (b instanceof B)
	    System.out.println("b is instance of B");
	if (c instanceof C)
	    System.out.println("c is instance of C");
	if (c instanceof A)
	    System.out.println("c can be cast to A");
	if (a instanceof C)
	    System.out.println("a can be cast to C");
	System.out.println();

    }

}

class A {
    int i, j;
}

class B {
    int i, j;
}

class C extends A {
    int k;
}

class D extends A {
    int k;
}

 

Output:
What are the legal operands of the instanceof operator

Leave a Reply

Your email address will not be published. Required fields are marked *