Java instanceof operator

Java Programming Java8Java Technologies Object Oriented Programming

instanceof operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as −

( Object reference variable ) instanceof  (class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is an example −

Example

Online Demo

public class Test {
   public static void main(String args[]) {
      String name = "James";
      // following will return true since name is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}

Output

This will produce the following result −

true
raja
Published on 22-Jan-2018 06:17:55
Advertisements