AddThis Social Bookmark Button

Everybody knows the instanceof operator, funny that an operator always like a symbol , but this one is not in ,Java. instanceof is pretty known, if you already know the language,but there are some less known fact which I am going to cover later in this section ,

but first just the basics and the possibilities exporing how to use instanceof operator
 

If you want to check the non-trivial part click here.

I made a Multiple choice question,for you too, test yourslef

An instanceof operator is an operator which takes two operands , the first is the object and the second is the type. And the statement evaluates to a boolean

Syntax: <operand1> instanceof <operand2>

Lets take and example

String s="a string";
if(s instanceof String){
System.out.println("Yes it is");
}else{
System.out.println("No is is not");
}

So we are checking if object s is an instance of String.

Not to be surprised with a literal.

if("a String" instanceof String){
System.out.println("Yes it is");
}else{
System.out.println("No is is not");
}

This one evaluates to "Yes it is".

Lets see what else is possible.

As I mentioned earlier the second operand is a type , any Java type, Class,Interface and Enums, Well not the basics data types. That means you can't use the byte,short,int,long etc as second operand, But Integer,Byte,Short etc are possible because they are Class types. So similarly the Interface and Enums are also possible, lets take and example.

enum MyEnum {
	TEST,FEST
}

 class MyClass{
	public static void main(String ads[]){
		if(MyEnum.FEST instanceof MyEnum){
			System.out.println("Yes it is");
			}else{
			System.out.println("No is is not");
			}
	}
}

It would output as "Yes it is" , pretty straight forward, MyEnum.FEST is an instance of MyEnum, Lets see an example with an interface.

interface Domestic {}

interface Foreign{}

public class MyClass implements Domestic , Foreign{
	public static void main(String ads[]){
		MyClass mc= new MyClass();
		if(mc instanceof Domestic){
			System.out.println("mc object is an instance of Domestic");
		}
		if(mc instanceof Foreign){
			System.out.println("mc object is also an instance of Foreign");
		}
               if(mc instanceof MyClass){
                 System.out.println("mc object is also an instance of MyClass,duh this is obvious");
                }
	}
}

A no brainer really , the mc object is instance of all the three,Domestic,Foreign and MyClass.

Now We know everything basic ,lets see the odd part the non-trivial part.
 When using instanceof operator , make sure the two operands are compatible. The object must be a type or subtype of the second operand otherwise a compilation problem will be reported.
To make it more comprehendable lets take an example

class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public class MyClass{
    public static void main(String ads[]){
            if(new Dog() instanceof Animal){
               System.out.println("Dog is an animal");
            }           
            if( new Cat() instanceof Dog){
                System.out.println("True");
            }else{
                System.out.println(" False");
            }
    }
}

 

The above line where we are testing dog with an Animal is fine, which will lead to "Dog is an Animal", But the Cat with Dog testing will not lead to an output of "False" contrary to the belive. It will be a compilation problem at line 9.

I will  leave you alone, just test yourself if you learnt things right, go test yourselves