Summary of the difference between instanceof and getClass() in JAVA

When comparing between objects, it is necessary to compare the class attribute values ​​of the two objects. At this time, instanceof or getClass() can be selected for comparison and judgment. Both can be used to compare whether the classes are the same between objects, but there are some differences.

instanceof

When using instanceof, it will judge whether the object belongs to the specified class or interface, and also judge whether it belongs to its inherited class or implementation class. Returns true if one of the conditions is met, false otherwise. The test code is as follows:

public class Test {

    public static void main(String[] args) {
        Object a = new SubClassA();

        System.out.println("a instanceof SubClassA is: " + (a instanceof SubClassA));
        System.out.println("a instanceof SuperClass is: " + (a instanceof SuperClass));
        System.out.println("a instanceof SuperInterface is: " + (a instanceof SuperInterface));
        System.out.println("a instanceof SubClassB is: " + (a instanceof SubClassB));
    }
}

class SuperClass {

}

interface SuperInterface {

}

class SubClassA extends SuperClass implements SuperInterface {

}

class SubClassB extends SuperClass {

}

The above code is entered as follows:

a instanceof SubClassA is: true
a instanceof SuperClass is: true
a instanceof SuperInterface is: true
a instanceof SubClassB is: false

Since object a is an instance of SubClassA class, inherits SuperClass class, and implements SuperInterface interface, the result is true when using instanceof to judge, but it is false when comparing with SubClassB class.

getClass()

When using the getClass() method to judge, because the getClass() method returns the Class object whose value is the current instance object, and the Class class does not override the equals() method, so whether to use == or equals() when judging The methods have the same effect (the equals() method of the Object class also uses == for comparison), which is to compare whether the current Class objects are the same. The test code is as follows:

    public static void main(String[] args) {
        Object a = new SubClassA();

        System.out.println("a.getClass() == SubClassA.class is: " + (a.getClass() == SubClassA.class));
        System.out.println("a.getClass() == SuperClass.class is: " + (a.getClass() == SuperClass.class));
        System.out.println("a.getClass() == SuperInterface.class is: " + (a.getClass() == SuperInterface.class));
        System.out.println("a.getClass() == SubClassB.class is: " + (a.getClass() == SubClassB.class));
    }

The above code is entered as follows:

a.getClass() == SubClassA.class is: true
a.getClass() == SuperClass.class is: false
a.getClass() == SuperInterface.class is: false
a.getClass() == SubClassB.class is: false

Since the a object is an instance of the SubClassA class, its parent class or its interface class will not be associated when judging by the getClass() method, so the corresponding judgment results are all false.

It can be seen from the above comparison that when using instanceof to judge a class, as long as the object on the left is a subclass or an implementation class of the class on the right, it can be established. When using the getClass() method to judge, it is necessary to strictly judge whether it is an object of the same class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325360039&siteId=291194637