每日一博 - instanceof vs isInstance vs isAssignableFrom

简介: operator instanceof;method Class.isInstance(Object obj);method Class.isAssignableFrom(Class<?> cls);

@[toc]

在这里插入图片描述


Pre

  • operator instanceof;
  • method Class.isInstance(Object obj);
  • method Class.isAssignableFrom(Class<?> cls);

isInstance

    /**
     * Determines if the specified {@code Object} is assignment-compatible
     * with the object represented by this {@code Class}.  This method is
     * the dynamic equivalent of the Java language {@code instanceof}
     * operator. The method returns {@code true} if the specified
     * {@code Object} argument is non-null and can be cast to the
     * reference type represented by this {@code Class} object without
     * raising a {@code ClassCastException.} It returns {@code false}
     * otherwise.
     *
     * <p> Specifically, if this {@code Class} object represents a
     * declared class, this method returns {@code true} if the specified
     * {@code Object} argument is an instance of the represented class (or
     * of any of its subclasses); it returns {@code false} otherwise. If
     * this {@code Class} object represents an array class, this method
     * returns {@code true} if the specified {@code Object} argument
     * can be converted to an object of the array class by an identity
     * conversion or by a widening reference conversion; it returns
     * {@code false} otherwise. If this {@code Class} object
     * represents an interface, this method returns {@code true} if the
     * class or any superclass of the specified {@code Object} argument
     * implements this interface; it returns {@code false} otherwise. If
     * this {@code Class} object represents a primitive type, this method
     * returns {@code false}.
     *
     * @param   obj the object to check
     * @return  true if {@code obj} is an instance of this class
     *
     * @since JDK1.1
     */
    public native boolean isInstance(Object obj);

isAssignableFrom

    /**
     * Determines if the class or interface represented by this
     * {@code Class} object is either the same as, or is a superclass or
     * superinterface of, the class or interface represented by the specified
     * {@code Class} parameter. It returns {@code true} if so;
     * otherwise it returns {@code false}. If this {@code Class}
     * object represents a primitive type, this method returns
     * {@code true} if the specified {@code Class} parameter is
     * exactly this {@code Class} object; otherwise it returns
     * {@code false}.
     *
     * <p> Specifically, this method tests whether the type represented by the
     * specified {@code Class} parameter can be converted to the type
     * represented by this {@code Class} object via an identity conversion
     * or via a widening reference conversion. See <em>The Java Language
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
     *
     * @param cls the {@code Class} object to be checked
     * @return the {@code boolean} value indicating whether objects of the
     * type {@code cls} can be assigned to objects of this class
     * @exception NullPointerException if the specified Class parameter is
     *            null.
     * @since JDK1.1
     */
    public native boolean isAssignableFrom(Class<?> cls);

isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角度去判断。
isAssignableFrom()方法是判断是否为某个类的父类,instanceof关键字是判断是否某个类的子类。
父类.class.isAssignableFrom(子类.class)

子类实例 instanceof 父类类型
相关文章
|
2月前
|
安全 Java Python
instanceof 的实现原理
`instanceof` 是 Java 中的一个关键字,用于判断一个对象是否属于某个类或其子类。其原理是通过检查对象的类层次结构,确定该对象是否是指定类的实例。具体实现涉及对象头中的类元数据信息和类加载器的作用。
|
8月前
|
Java
instanceof是什么~
instanceof是什么~
|
Java
关键字instanceof
关键字instanceof
47 0
|
8月前
|
Java
JavaSE基础篇:instanceof关键字
JavaSE基础篇:instanceof关键字
|
存储 Java 开发者
使用Optional优雅避免空指针异常
在编程世界中, 空指针异常(NullPointerException) 无疑是我们最常遇到的"罪魁祸首"之一。它像一片隐蔽的地雷,静静地等待着我们不小心地踏入,给我们的代码带来潜在的威胁。这种问题虽然看似微小,但却无法忽视。甚至可能对整个程序的稳定性产生重大影响。
73 0
|
JavaScript 前端开发
每日一题:typeof 与 instanceof 区别
每日一题:typeof 与 instanceof 区别
102 0