java isAssignableFrom instanceof 小结 专题
一句话总结:
isAssignableFrom()方法是从类继承的角度去判断。当前类及子类都返回true。父类及接口返回false
instanceof方法是从实例继承的角度去判断。一个类的实例 是否实现某个接口,是不是实例对应类、实例对应类的父类
instanceof和isInstance 起的效果相同。
数组与上面的效果相同
基础数据:
@Data
public static class ParentClass implements Serializable {
private String name;
} @Data
@EqualsAndHashCode(callSuper = true)
public static class OneLevelChildClass extends ParentClass {
private String oneLevelChildName;
} @Data
@EqualsAndHashCode(callSuper = true)
public static class TwoLevelChildClass extends OneLevelChildClass {
private String twoLevelChildName;
}
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化。
如果被测对象是null值,则测试结果总是false。
形象地:自身实例或子类实例 instanceof 自身类 返回true
例:
String s=new String("javaisland");
System.out.println(s instanceof String); //true
@Test
public void testInstanceof() {
/**
* instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化。
* 如果被测对象是null值,则测试结果总是false。
*/
ParentClass parentClass = new ParentClass();
assertThat(parentClass instanceof Serializable).isEqualTo(true);
assertThat(parentClass instanceof ParentClass).isEqualTo(true);
assertThat(parentClass instanceof OneLevelChildClass).isEqualTo(false);
assertThat(parentClass instanceof TwoLevelChildClass).isEqualTo(false); OneLevelChildClass oneLevelChildClass = new OneLevelChildClass();
assertThat(oneLevelChildClass instanceof Serializable).isEqualTo(true);
assertThat(oneLevelChildClass instanceof ParentClass).isEqualTo(true);
assertThat(oneLevelChildClass instanceof OneLevelChildClass).isEqualTo(true);
assertThat(oneLevelChildClass instanceof TwoLevelChildClass).isEqualTo(false); TwoLevelChildClass twoLevelChildClass = new TwoLevelChildClass();
assertThat(twoLevelChildClass instanceof Serializable).isEqualTo(true);
assertThat(twoLevelChildClass instanceof ParentClass).isEqualTo(true);
assertThat(twoLevelChildClass instanceof OneLevelChildClass).isEqualTo(true);
assertThat(twoLevelChildClass instanceof TwoLevelChildClass).isEqualTo(true);
}
Class类的isInstance(Object obj)方法,obj是被测试的对象,如果obj是调用这个方法的class或子类或接口 的实例,则返回true。
这个方法是instanceof运算符的动态等价。
例:
@Test
public void testIsInstance() {
/**
* 作用范围与instanceof相同
*/
ParentClass parentClass = new ParentClass();
assertThat(Serializable.class.isInstance(parentClass)).isEqualTo(true);
assertThat(ParentClass.class.isInstance(parentClass)).isEqualTo(true);
assertThat(OneLevelChildClass.class.isInstance(parentClass)).isEqualTo(false);
assertThat(TwoLevelChildClass.class.isInstance(parentClass)).isEqualTo(false); OneLevelChildClass oneLevelChildClass = new OneLevelChildClass();
assertThat(Serializable.class.isInstance(oneLevelChildClass)).isEqualTo(true);
assertThat(ParentClass.class.isInstance(oneLevelChildClass)).isEqualTo(true);
assertThat(OneLevelChildClass.class.isInstance(oneLevelChildClass)).isEqualTo(true);
assertThat(TwoLevelChildClass.class.isInstance(oneLevelChildClass)).isEqualTo(false); TwoLevelChildClass twoLevelChildClass = new TwoLevelChildClass();
assertThat(Serializable.class.isInstance(twoLevelChildClass)).isEqualTo(true);
assertThat(ParentClass.class.isInstance(twoLevelChildClass)).isEqualTo(true);
assertThat(OneLevelChildClass.class.isInstance(twoLevelChildClass)).isEqualTo(true);
assertThat(TwoLevelChildClass.class.isInstance(twoLevelChildClass)).isEqualTo(true);
}
Class类的isAssignableFrom(Class cls)方法,如果调用这个方法的class或接口 与 参数cls表示的类或接口相同,或者是参数cls表示的类或接口的父类,则返回true。
形象地:自身类.class.isAssignableFrom(自身类或子类.class) 返回true
例:
@Test
public void testIsAssignableFrom() {
ParentClass parentClass = new ParentClass();
assertThat(parentClass.getClass().isAssignableFrom(Serializable.class)).isEqualTo(false);
/**
* 当前类及子类都返回true。父类及接口返回false
*/
assertThat(parentClass.getClass().isAssignableFrom(ParentClass.class)).isEqualTo(true);
assertThat(parentClass.getClass().isAssignableFrom(OneLevelChildClass.class)).isEqualTo(true);
assertThat(parentClass.getClass().isAssignableFrom(TwoLevelChildClass.class)).isEqualTo(true); OneLevelChildClass oneLevelChildClass = new OneLevelChildClass();
assertThat(oneLevelChildClass.getClass().isAssignableFrom(Serializable.class)).isEqualTo(false);
assertThat(oneLevelChildClass.getClass().isAssignableFrom(ParentClass.class)).isEqualTo(false);
/**
* 当前类及子类都返回true。父类及接口返回false
*/
assertThat(oneLevelChildClass.getClass().isAssignableFrom(OneLevelChildClass.class)).isEqualTo(true);
assertThat(oneLevelChildClass.getClass().isAssignableFrom(TwoLevelChildClass.class)).isEqualTo(true); TwoLevelChildClass twoLevelChildClass = new TwoLevelChildClass();
assertThat(twoLevelChildClass.getClass().isAssignableFrom(Serializable.class)).isEqualTo(false);
assertThat(twoLevelChildClass.getClass().isAssignableFrom(ParentClass.class)).isEqualTo(false);
assertThat(twoLevelChildClass.getClass().isAssignableFrom(OneLevelChildClass.class)).isEqualTo(false);
/**
* 当前类及子类都返回true。父类及接口返回false
*/
assertThat(twoLevelChildClass.getClass().isAssignableFrom(TwoLevelChildClass.class)).isEqualTo(true);
}
System.out.println(ArrayList.class.isAssignableFrom(Object.class)); //false
System.out.println(Object.class.isAssignableFrom(ArrayList.class)); //true
System.out.println("String是Object的父类:"+String.class.isAssignableFrom(Object.class)); //false
System.out.println("Object是String的父类:"+Object.class.isAssignableFrom(String.class)); //true
System.out.println("Object和Object相同:"+Object.class.isAssignableFrom(Object.class)); //true
https://www.cnblogs.com/exmyth/p/3164492.html
https://www.cnblogs.com/bethunebtj/p/4681438.html
@Test
public void testArrayInClassMethod() {
Integer[] intTypes = {1, 2};
assertThat(intTypes.getClass().isArray()).isTrue();
assertThat(intTypes instanceof Integer[]).isTrue();
// assertThat(intTypes instanceof int[]); //会编译报错:不兼容的类型
assertThat(intTypes instanceof Object[]).isTrue(); assertThat(Integer[].class.isInstance(intTypes)).isTrue();
assertThat(Object[].class.isInstance(intTypes)).isTrue();
// assertThat(intTypes instanceof String[]).isTrue();//会编译报错:不兼容的类型
assertThat(intTypes.getClass().isAssignableFrom(Object[].class)).isFalse();
assertThat(intTypes.getClass().isAssignableFrom(Integer[].class)).isTrue(); /**
* 当前类及子类都返回true。父类及接口返回false
*/
assertThat(intTypes.getClass().isAssignableFrom(Object[].class)).isFalse();
assertThat(intTypes.getClass().isAssignableFrom(Integer[].class)).isTrue(); Object[] objects = {1, 2};
assertThat(objects instanceof Integer[]).isFalse();
assertThat(objects instanceof Object[]).isTrue(); assertThat(Integer[].class.isInstance(objects)).isFalse();//具体到抽象的为false
assertThat(Object[].class.isInstance(objects)).isTrue(); /**
* 当前类及子类都返回true。父类及接口返回false
*/
assertThat(objects.getClass().isAssignableFrom(Object[].class)).isTrue();
assertThat(objects.getClass().isAssignableFrom(Integer[].class)).isTrue();
}
/**
* 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);
/**
* 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);
org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver#isEmptyPayload
/**
* Specify if the given {@code payload} is empty.
* @param payload the payload to check (can be {@code null})
*/
protected boolean isEmptyPayload(@Nullable Object payload) {
if (payload == null) {
return true;
}
else if (payload instanceof byte[]) {
return ((byte[]) payload).length == 0;
}
else if (payload instanceof String) {
return !StringUtils.hasText((String) payload);
}
else {
return false;
}
}
java isAssignableFrom instanceof 小结 专题的更多相关文章
- java中instanceof和getClass()的作用
初学者难免有点混淆java中instanceof和getClass()的作用, 下面就来一一讲解. 父类A: class A { } 子类B: class B extends A { } 构造对象 ...
- 深入Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interface ...
- java并发包小结(二)
接上一篇 java并发包小结(一):http://blog.csdn.net/aalansehaiyang52/article/details/8877579 Future 接口Future 接口允许 ...
- Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法
ylbtech-Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法 1.返回顶部 1. Java 实例 - instanceof 关键字用法 ...
- Java关键字instanceof
深入Java关键字instanceof instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例. 举个例子: public interfa ...
- Java中instanceof和isInstance的具体区别
Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...
- Java 实例 - instanceof 关键字用法
Java 实例 - instanceof 关键字用法 instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符. instanceof 是 Java 的保留关键 ...
- java IO 流小结
java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.
- 图灵学院JAVA互联网架构师专题学习笔记
图灵学院JAVA互联网架构师专题学习笔记 下载链接:链接: https://pan.baidu.com/s/1xbxDzmnQudnYtMt5Ce1ONQ 密码: fbdj如果失效联系v:itit11 ...
随机推荐
- 【转载】String和StringBuffer的区别,以及StringBuffer的常用方法介绍
String与StringBuffer的区别简单地说,就是一个变量和常量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象.Stri ...
- Thrift之代码生成器Compiler原理及源码详细解析1
我的新浪微博:http://weibo.com/freshairbrucewoo. 欢迎大家相互交流,共同提高技术. 又很久没有写博客了,最近忙着研究GlusterFS,本来周末打算写几篇博客的,但是 ...
- 苹果app(iOS app)的URL schemes
最近折腾iOS快捷启动应用或应用内的某个动作的神器launch center pro (LCP),发现很多国产app并没有被LCP官方收录,所以不得不想办法找到app的url schemes. 下面是 ...
- CSS:CSS 颜色
ylbtech-CSS:CSS 颜色 1.返回顶部 1. 颜色是通过对红.绿和蓝光的组合来显示的. 颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光 ...
- 利用Dockerfile文件创建带有sshd服务的centos镜像
利用Dockerfile文件创建带有sshd服务的centos镜像 标签:dockerfile 1.安装docker并启动docker,不在赘述 2.创建使用Dockerfile安装sshd服务的目录 ...
- 激光SLAM
1.激光分类 维度分类: a.二维激光(单点反射.平面.旋转台) b.三维激光 距离分类: a.近距离:壁障.碰撞检测.路边检测 b.远距离:行人检测.定位.建图 特点: 优点: a.可以直接获取深度 ...
- Cannot set headers after they are sent to the client
D:\le\node_modules\mysql\lib\protocol\Parser.js: throw err; // Rethrow non-MySQL errors ^ Error [ERR ...
- lightoj1066【BFS】
题意: 就是按照A->B->C->D....去拿,求步数: 思路: 有一个注意点:如果碰到合法字母吃掉,再以后的某次可以重新到改点: BFS的因为标记而减少了重复位置的到达,但是按照 ...
- Codeforces Round #377 (Div. 2)A,B,C,D【二分】
PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...
- 正向渲染路径细节 Forward Rendering Path Details
http://www.ceeger.com/Components/RenderTech-ForwardRendering.html This page describes details of For ...