instanceof Java Keyword

Introduction

The instanceof keyword is very useful to know whether an object belongs to which class. The instanceof evaluates to true if an object belongs to a specified class or its super class; else raises compilation error. The following program illustrates.

public class InstanceDemo
{
  public static void main(String args[])
  {
    String s1 = "hello";
    Double d1 = new Double(10.5);
    InstanceDemo id = new InstanceDemo();

    if(s1 instanceof String)
      System.out.println("s1 is an object of String class");
    if(d1 instanceof Double)
      System.out.println("d1 is an object of Double class");
    if(id instanceof InstanceDemo)
      System.out.println("id is an object of InstanceDemo class");
       //  if(d1 instanceof Integer
       //   System.out.println("d1 is not Integer object");
     }
}


instanceof Java Keyword
Output screenshot of instanceof Java Keyword

The objects s1, d1 and id are checked whether they belong to String, a Double or InstanceDemo class.

The instanceof keyword evaluates to true even when the object is checked with its super class.

class Test  {    }
class Hello  extends Test   {    }
public class SuperClassCheck extends Hello   
{    
  public static void main(String args[])
  {
    SuperClassCheck scc = new SuperClassCheck();
    if(scc instanceof SuperClassCheck)
      System.out.println("scc is an object of SuperClassCheck");
    if(scc instanceof Hello)
      System.out.println("scc is an object of Hello");
    if(scc instanceof Test)
      System.out.println("scc is an object of Test");
    if(scc instanceof Object)
      System.out.println("scc is an object of Object");

    Integer i1 = null;
    System.out.println(i1 instanceof Integer);
  }
}


instanceof Java Keyword
Output screenshot of instanceof Java Keyword

scc is an object of SuperClassCheck. The scc object is checked with the classes SuperClassCheck, Hello and Test. All are evaluated to true as Hello and Test are super classes.

if(scc instanceof Object)

The above statement also evaluates to true as Object class is implicit super class of all Java objects.

Think that you know there is an object that belongs to some wrapper class. You can find out for which wrapper class the object belongs. The following program illustrates.

public class KnowYourClass
{
  public static void main(String args[])
  {
    Double d1 = new Double(20.6);
    Object obj = d1;
		               // check with each wrapper class
    if(obj instanceof Byte)
       System.out.println("d1 is of Byte class");
    if(obj instanceof Short)
       System.out.println("d1 is of Short class");
    if(obj instanceof Integer)
       System.out.println("d1 is of Integer class");
    if(obj instanceof Long)
       System.out.println("d1 is of Long class");
    if(obj instanceof Float)
       System.out.println("d1 is of Float class");
    if(obj instanceof Double)
       System.out.println("d1 is of Double class");
    if(obj instanceof Character)
       System.out.println("d1 is of Character class");
    if(obj instanceof Boolean)
       System.out.println("d1 is of Boolean class");
  }	
}


instanceof Java Keyword
Output screenshot of instanceof Java Keyword

Double d1 = new Double(20.6);
Object obj = d1;

Here, a small programming technique is implemened. The Double object d1 is assigned to obj of Object class. Now, obj contains the reference of Double. The Double object d1 is checked with every wrapper class.

instanceOf

There does not exist an instanceof method in Java language as such. This search term by the novice can be interpreted as "instanceof used in a method"; the method finds out to which class the object passed as parameter belongs. In the following code, an Object class parameter is passed to the method findOut() and in the body checked for which class this object belongs.

class Test
{
  int x = 100;
}
class Hello
{
  int y = 200;
}
public class Demo
{
  public void findOut(Object obj)
  {
    if(obj instanceof Test)
    {
      System.out.println("In the obj, the reference of Test exists.");
      Test t2 = (Test) obj;
      System.out.println(t2.x);
    }
    else if(obj instanceof Hello)
    {
      System.out.println("in the obj, the reference of Hello exists.");
      Hello h2 = (Hello) obj;
      System.out.println(h2.y);
    }
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    Test t1 = new Test();
    d1.findOut(t1);
    
    Hello h1 = new Hello();
    d1.findOut(h1);
  }
}

To the findOut() method two objects are passed, at different times, and found out for which class Test or Hello, the objects t1 and h1 belongs. As per the instanceof keyword evaluation, the variables x or y are called and printed.

instanceOf Interface

In the above program, the Test and Hello can be interfaces and still the program executes smoothly.

interface Test
{
  int x = 100;
}

class ConcreteTest implements Test  {    }

interface Hello
{
  int y = 200;
}

class ConcreteHello implements Hello  {    }

public class Demo
{
  public void findOut(Object obj)
  {
    if(obj instanceof Test)
    {
      System.out.println("In the obj, the reference of Test exists.");
      Test t2 = (Test) obj;
      System.out.println(t2.x);
    }
    else if(obj instanceof Hello)
    {
      System.out.println("in the obj, the reference of Hello exists.");
      Hello h2 = (Hello) obj;
      System.out.println(h2.y);
    }
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
            
    ConcreteTest t1 = new ConcreteTest();
    d1.findOut(t1);

    ConcreteHello h1 = new ConcreteHello();
    d1.findOut(h1);
  }
}

Between two interfaces, extends keyword should be used.

7 thoughts on “instanceof Java Keyword”

  1. Hello Sir,

    In the instanceOf method example we have Hello, Test and Demo as 3 different classes. We did not inherit Demo from either Hello or Test, then how it is possible to create the objects inside the Demo class.

    Plz explain.

  2. I blog frequently and I seriously appreciate your information.
    This article has really peaked my interest.
    I will bookmark your website and keep checking for new details about once a week.
    I subscribed to your Feed too.

  3. Hey there! Would you mind if I share your blog with my facebook group?

    There’s a lot of folks that I think would really enjoy your content. Please let me know. Thanks

Leave a Comment

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