When to use Class.isInstance() & when to use instanceof operator?
I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific way):
This method is the dynamic equivalent of the Java language instanceof operator.
I take that to mean that isInstance() is primarily intended for use in code dealing with type reflection at runtime. In particular, I would say that it exists to handle cases where you might not know in advance the type(s) of class(es) that you want to check for membership of in advance (rare though those cases probably are).
For instance, you can use it to write a method that checks to see if two arbitrarily typed objects are assignment-compatible, like:
publicboolean areObjectsAssignable(Object left,Object right){return left.getClass().isInstance(right);}
In general, I'd say that using instanceof should be preferred whenever you know the kind of class you want to check against in advance. In those very rare cases where you do not, use isInstance() instead.
For instanceof you need to know the exact class at compile time.
if(foo instanceofThisClassIKnowRightNow)...
For isInstance the class is decided at run time. (late binding) e.g.
if(someObject.getClass().isInstance(foo))...
When to use Class.isInstance() & when to use instanceof operator?的更多相关文章
- java instanceof和isInstance的关系 精析
1.instanceof 用途:判断这个object对象是不是这种Class类型. 语法: boolean result = object instanceof Class; 用法: 判断obje ...
- Java中instanceof和isInstance的具体区别
Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...
- Java instanceof 和 Class.isInstance()区别与应用
一.instanceof 关键字 instanceof 关键字用于判断某个实例是否是某个类的实例化对象,形如: String.class instanceof Class "test&quo ...
- Akka框架使用注意点
1.mailbox Akka的每个actor默认有一个mailbox,按照FIFO顺序单线程处理.在抛出异常导致父actor根据设置的监管策略执行重启或恢复操作时,会从触发异常的消息的后续消息开始处理 ...
- [zz]Java中的instanceof关键字
1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...
- 从fastjson多层泛型嵌套解析,看jdk泛型推断
给你一组json数据结构,你把它解析出来到项目中,你会怎么做? // data1 sample { "code" : "1", "msg" ...
- 判断一个类是否为另一个类的实例 instanceof关键字和isAssignableFrom方法的区别
Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only ...
- Java RTTI and Reflection
Reference: Java编程思想 java 反射(Reflect) Java系列笔记(2) - Java RTTI和反射机制 Java Reflection in Action, 有空再补 -- ...
- 反射01 Class类的使用、动态加载类、类类型说明、获取类的信息
0 Java反射机制 反射(Reflection)是 Java 的高级特性之一,是框架实现的基础. 0.1 定义 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...
随机推荐
- Javascript中二级联动
主要使用到到了地址JSON格式,来做,没有涉及数据库的读取. <!DOCTYPE html><html><head> <meta charset=" ...
- 关于HTML中浮动与清除的思考
布局时需要利用浮动(float)的属性,同时我们需要一个清除浮动(clear)与之配合才能达到预期的目标. w3s上关于float和clear的定义分别为:float:float 属性定义元素在哪个方 ...
- 【CSS3】---first-of-type选择器+nth-of-type(n)选择器
first-of-type选择器 “:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子元素. 示 ...
- @@Error使用简单小结
使用中经常用到@@Error来判断上一个语句是否执行成功,对此小结一下,可能有些不准确,欢迎指出. 1.1 介绍 SQL SERVER 中@@表示系统全局变量 (1) 返回执行的上一个 Tran ...
- Python Opearte SQLAlchemy Do Something
近段时间在看SQLAlchemy,总之万事开头难,但是么办法. Database Urls The create_engine() function produces an Engine object ...
- win7 开启休眠
使用cmd命令进行开启,首先点击开始菜单,在“搜索程序和文件”中输入“cmd”,然后点击回车键.如下: 2 弹出如下图的界面,在其中最后的地方输入“powercfg -hibernate on”,然后 ...
- APUE习题8.7
看书的时候发现这个习题没有答案,于是就想把自己做的结果贴上来,和大家分享分享! 首先把题目贴上来吧: /*********** 8.10节中提及POSIX.1要求在调用exec时关闭打开的目录流.按下 ...
- 主引导记录MBR
作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...
- DevExpress控件水印文字提示
ButtonEdit.Properties.NullValuePrompt = "提示"; ButtonEdit.Properties.NullValuePromptShowFor ...
- 基础学习总结(七)--子线程及Handler
使用子线程获取网络图片1.采用httpUrlConnection直连方式获取图片2.采用子线程方式获取 <LinearLayout xmlns:android="http://sche ...