Talk about isAssignableFrom () method with instanceof () method difference?

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43719015/article/details/102719342

Sentence summary:

  • IsAssignableFrom () method is inherited from class angle to judge, instanceof () method is inherited from an instance to an angle determination.
  • IsAssignableFrom () method is to determine whether a parent class, instanceof () method is to determine whether a subclass of a class.

1. Class.isAssignableFrom()方法

Class.isAssignableFrom () is used to determine a sub-class or interface class and another class Class1 or Class2 is identical to another class.
The format is:

Class1.isAssignableFrom(Class2) 

The caller and parameters are java.lang.Class type.

2.Class.instanceof () method
Class.instanceof () is used to determine whether an object instance is an instance of a class or interface, or a sub-sub-class interface.
The format is:

obj instanceof TypeName  

The first parameter is the name of the object instance, the second parameter is specific to the class or interface name, e.g. String, InputStream. Whose return value boolean.

Case:

public class AssignableTest {
    public AssignableTest(String name) {
    }
    /**
     * 判断一个类是否是另一个类的父类
     * 是打印true
     * 否打印false
     */
    public static void testIsAssignedFrom1() {
        System.out.println("String是Object的父类:"+String.class.isAssignableFrom(Object.class));
    }
    /**
     * 判断一个类是否是另一个类的父类
     * 是打印true
     * 否打印false
     */
    public static void testIsAssignedFrom2() {
        System.out.println("Object是String的父类:"+Object.class.isAssignableFrom(String.class));
    }
    /**
     * 判断一个类是否和另一个类相同
     * 是打印true
     * 否打印false
     */
    public static void testIsAssignedFrom3() {
        System.out.println("Object和Object相同:"+Object.class.isAssignableFrom(Object.class));
    }

    /**
     * 判断str是否是Object类的实例
     * 是打印true
     * 否打印false
     */
    public static void testInstanceOf1() {
        String str = new String();
        System.out.print("str是Object的实例:");
        System.out.println(str instanceof Object);
    }
    /**
     * 判断o是否是Object类的实例
     * 是打印true
     * 否打印false
     */
    public static void testInstanceOf2() {
        Object o = new Object();
        System.out.print("o是Object的实例:");
        System.out.println(o instanceof Object);
    }

    public static void main(String[] args) {
        testIsAssignedFrom1(); // String是Object的父类:false
        testIsAssignedFrom2(); // Object是String的父类:true
        testIsAssignedFrom3();// Object和Object相同:true
        testInstanceOf1(); // str是Object的实例:true
        testInstanceOf2(); // o是Object的实例:true
    }
}

Reference links:
the Java in isAssignableFrom () method with instanceof () method usage

Guess you like

Origin blog.csdn.net/weixin_43719015/article/details/102719342