devxlogo

Using the instanceof Keyword in Java

Using the instanceof Keyword in Java

When you are typecasting and using instanceof keyword in Java, there are several things you should keep in mind. Consider the following example program:

 class Point { int x, y; }class Element { int atomicNumber; }class Test {    public static void main(String[] args) {        Point p = new Point();        Element e = new Element();        if (e instanceof Point) { // compile-time error            System.out.println(

devx-admin

Share the Post: