Java-Instanceof keyword

Instanceof keyword

The instanceof is used to knowing the type of an object during run time. The instanceof operator has this general form :

ref instanceof type

Here, ‘ref’ is a reference to an instance of a class, and ‘type’ is a class type. If ‘ref’ is of the specified type or can be cast into the specified type, then the instanceof operator evaluates to true. Otherwise, its result is false. Thus, instanceof is the means by which your program can obtain run-time type information about an object.



Program Source

class DataA {


public class Javaapp {
    public static void main(String[] args) {
        DataA a = new DataA();
        if(a instanceof DataA)
            System.out.println("a instance of DataA");
        if(a instanceof Object)
            System.out.println("a extends Object");

Leave a Comment