instanceof operator in Java

Java provides a special operator known as ‘instanceof‘ operator.

This operator helps us to determine whether an object belongs to a particular class or not.

class Demo
{
  void fun()
  {
    System.out.println("Demo !!!");
  }
}

public class NewMain
{
  public static void main(String[] args)
  {
    Demo d = new Demo();

    if(d instanceof Demo)
    {
      System.out.println("Yes D is an Object of Class Demo.");
    }
    else
    {
      System.out.println("No D is an Object of Class Demo.");
    }
  }
}

/*
OUTPUT :

Yes D is an Object of Class Demo.

*/

Related Post

Leave a Reply

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