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. poj 3980 取模运算

    取模运算 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 6618 Description ...

  2. ASP.NET缓存全解析5:文件缓存依赖 转自网络原文作者李天平

    这种策略让缓存依赖于一个指定的文件,通过改变文件的更新日期来清除缓存. ///<summary> /// 获取当前应用程序指定CacheKey的Cache对象值 ///</summa ...

  3. Ubuntu 15.04 无损扩展分区(目录)容量的方法 (无需格式化, 文件不丢失)

    源 起 用了一段时间Ubuntu,碰到了UBuntu磁盘空间不足的问题, 最初我只给Ubuntu分配了30个G的空间, 昨天试用了一下VirtualBox安装了一个xp虚拟系统,用以解决Ubuntu下 ...

  4. windowSoftInputMode属性详解

    转自:http://blog.csdn.net/twoicewoo/article/details/7384398 activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Andro ...

  5. 十七、Android学习笔记_Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

  6. Part 1 some difference from asp.net to asp.net mvc4

    Part 1 some difference from asp.net to asp.net mvc4 In MVC URL's are mapped to controller Action Met ...

  7. JSP之错误信息提示

    MessageResource.properties配置文件: RegisterAction注册: package com.caiduping.action; import javax.servlet ...

  8. Cocos2d-x 3.x的Windows Phone 8工程

    Cocos2d-x 3.x中我们使用的Cocos2d-x 3.2,它提供了Windows Phone 8平台的支持.下面我们介绍一下在Cocos2d-x 3.2中如何生成Cocos2d-x的Windo ...

  9. iOS-KVC和KVO精炼讲解(干货)

    一.KVO介绍 KVO就是观察者模式,说白了就是你关心的一个值改变了,你就会得到通知.你就可以在你想处理的地方处理这个值. 二.KVO的使用 一般分为三步: 注册监听 使用方法: /** * 添加KV ...

  10. java中的生产者和消费者的问题

    1----使用Java.util.concurrent.locks包中的lock接口取代synchronized,通过ReentrantLock这个已经实现Lock接口的类, 创建ReentrantL ...