INFORMATION AND TECHNOLOGY

help facilitate knowledge sharing and knowledge management

    

JAVA

What is instanceof operator in java?

Instanceof is a binary operator in Java language, its role is to determine whether the object pointed to by the variable of a reference type is a class (or interfaces, abstract classes, parent class) instance that it left the object whether it is the right of an instance of a class, returns a Boolean data type.

Common usage is as follows: result = object instanceof class, if the object is an instance of class, then the instanceof operator returns true.If the object is not an instance of the specified class, or if object is null, return false.

In the following procedure, for example.public class Test {

public static void main(String args[]){

String s="Hello";

int[] a={1,2,3,4,5,6};

if(s instanceof String)

System.out.println("true");

if(s instanceof Object)

System.out.println("true");

if(a instanceof int[])

System.out.println("true");

}

}

Program results are:

true

true

true