Q:

Java program to demonstrate the instanceof operator

belongs to collection: Java Class and Object Programs

0

Java program to demonstrate the instanceof operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will check an object of an instance of a particular class or not using "instanceof" operator. It returns the Boolean value truefalse.

Program/Source Code:

The source code to demonstrate the "instanceof" operator is given below. The given program is compiled and executed successfully.

// Java program to demonstrate the example of 
// "instanceof" operator

public class Main {
  public static void main(String[] args) {
    Main m = new Main();

    boolean ret = m instanceof Main;

    if (ret)
      System.out.println("The Object m is an instance of Main class");
    else
      System.out.println("The Object m is not an instance of Main class");
  }
}

Output:

The Object m is an instance of Main class

Explanation:

In the above program, we created a class Main. The Main class contains a method main(). The main() method is an entry point for the program. Here, we created the object m of Main() class and checked 'm' is an instance of class Main or not using the "instanceof" operator and printed the appropriate message.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to create an anonymous object... >>
<< Java program to create an object of a class as a d...