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?的更多相关文章

  1. java instanceof和isInstance的关系 精析

      1.instanceof 用途:判断这个object对象是不是这种Class类型. 语法: boolean result = object instanceof Class; 用法: 判断obje ...

  2. Java中instanceof和isInstance的具体区别

    Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...

  3. Java instanceof 和 Class.isInstance()区别与应用

    一.instanceof 关键字 instanceof 关键字用于判断某个实例是否是某个类的实例化对象,形如: String.class instanceof Class "test&quo ...

  4. Akka框架使用注意点

    1.mailbox Akka的每个actor默认有一个mailbox,按照FIFO顺序单线程处理.在抛出异常导致父actor根据设置的监管策略执行重启或恢复操作时,会从触发异常的消息的后续消息开始处理 ...

  5. [zz]Java中的instanceof关键字

    1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...

  6. 从fastjson多层泛型嵌套解析,看jdk泛型推断

    给你一组json数据结构,你把它解析出来到项目中,你会怎么做? // data1 sample { "code" : "1", "msg" ...

  7. 判断一个类是否为另一个类的实例 instanceof关键字和isAssignableFrom方法的区别

    Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only ...

  8. Java RTTI and Reflection

    Reference: Java编程思想 java 反射(Reflect) Java系列笔记(2) - Java RTTI和反射机制 Java Reflection in Action, 有空再补 -- ...

  9. 反射01 Class类的使用、动态加载类、类类型说明、获取类的信息

    0 Java反射机制 反射(Reflection)是 Java 的高级特性之一,是框架实现的基础. 0.1 定义 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...

随机推荐

  1. web前端开发控件学习笔记之jqgrid+ztree+echarts

    版权声明:本文为博主原创文章,转载请注明出处.   作为web前端初学者,今天要记录的是三个控件的使用心得,分别是表格控件jqgrid,树形控件ztree,图表控件echarts.下边分别进行描述. ...

  2. js里正则表达式详解

    详细内容请点击 正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/ ...

  3. 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范

    HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 JavaScript 文件 实用为王 属性顺序 布尔(bool ...

  4. PLSQL执行sql语句输出的中文是???之解决方法和步骤

    方法/步骤 1 登陆plsql,执行sql语句,输出的中文标题显示成问号????:条件包含中文,则无数据输出 步骤阅读 2 输入sql语句select * from V$NLS_PARAMETERS查 ...

  5. 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult

    在本系列的最后一篇,我们来讨论最后三个ActionResult:HttpStatusCodeResult.RedirectResult和RedirectToRouteResult .第一个用于实现针对 ...

  6. (Android)处理图片成圆形

    Android将一张Bitmap处理成圆形是十分常见的,经常见的场合就是作为用户头像,我们可以Canvas来辅助实现这个功能,代码如下 public static Bitmap toRoundCorn ...

  7. 在sql设计中没法修改表结构

    在做练习的时候经常表没设计好,后来有要去数据库修改表结构但是没词用界面修改的时候都会提示要保存 转自http://www.57xue.com/ItemView/Sql/2016061600160.ht ...

  8. sql 查询包含字符的数量统计

    );); SELECT @word = 'I do not like to get the news, because there has never been an era when so many ...

  9. WordPress 去除图片img标签的高度与宽度

    要求 如,在桌面设备上,图片使用的是以下的HTML代码:  代码如下 复制代码 1  <img src="abc.png" alt="abc" width ...

  10. AOJ 0558 Cheese

    Cheese Time Limit : 8 sec, Memory Limit : 65536 KB チーズ (Cheese) 問題 今年も JOI 町のチーズ工場がチーズの生産を始め,ねずみが巣から ...