Easy Tutorial
❮ File Append Java9 Stream Api Improvements ❯

Java Example - instanceof Keyword Usage

Java Examples

instanceof is a binary operator in Java, similar to operators like ==, >, <, etc.

instanceof is a reserved keyword in Java. It is used to test whether the object on the left side is an instance of the class on the right side, returning a boolean data type.

The following example creates the displayObjectClass() method to demonstrate the usage of the Java instanceof keyword:

Main.java File Code:

/*
 author by tutorialpro.org
 Main.java
 */
import java.util.ArrayList;
import java.util.Vector;

public class Main {

public static void main(String[] args) {
   Object testObject = new ArrayList();
      displayObjectClass(testObject);
   }
   public static void displayObjectClass(Object o) {
      if (o instanceof Vector)
      System.out.println("Object is an instance of java.util.Vector class");
      else if (o instanceof ArrayList)
      System.out.println("Object is an instance of java.util.ArrayList class");
      else
      System.out.println("Object is an instance of " + o.getClass() + " class");
   }
}

The output of the above code is:

Object is an instance of java.util.ArrayList class

Java Examples

❮ File Append Java9 Stream Api Improvements ❯