ADVERTISEMENT
ADVERTISEMENT

Java program to demonstrate the instanceof operator

Write a Java program to demonstrate the example of instanceof operator.
Submitted by Nidhi, on March 24, 2022

Problem Solution:

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 true, false.

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.

Java Class and Object Programs »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.