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 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...
随机推荐
- revel + swagger 文档也能互动啦
beego 从 1.3 后开始支持自动化API文档,不过,目测比较复杂,一直期望 revel 能有官方支持. revel 确实已经有了官方支持的计划,有可能将在 0.14 版本支持,现在才 0.11. ...
- 关于asp.net MVC 中的TryUpdateModel方法
有比较才会有收货,有需求才会发现更多. 在传统的WebFormk开发工作中,我们常常会存在如下的代码块 //保存 protected void btnSubmit_Click(object sende ...
- C# Tips: Draw a data table in console
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- part 2 Angular modules and controllers
What is a module in AngularJS? A module is a container for different parts of your application i.e c ...
- WPF非UI线程获取修改控件属性值的方法
public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...
- pip在windows域下使用代理安装package方法
首先说明下,本人在公司使用windows域账户代理上网,用pip在线安装package 返回ProxyError,类似Tunnel connection failed: 407 authenticat ...
- C# 线程--第一单线程基础
概念 什么是进程? 当一个程序被打开运行时,它就是一个进程.在进程中包括线程,进程可以由一个或多个线程组成. 什么是线程? 线程是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC), ...
- sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序
1027. MJ, Nowhere to Hide Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description On BBS, th ...
- score
#include<iostream> using namespace std; class student{ public: int Input() { ;i<;i++) { cou ...
- 特殊的attribute机制
__attribute__机制是GNU C的一大特色,可以用来设置函数,变量和数据类型的属性,下面对工作中正好用到的两个属性做下简单介绍. 1. constructor 这个属性指定函数在main函数 ...