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?,布布扣,bubuko.com

When to use Class.isInstance() & when to use instanceof
operator?

时间: 2024-08-05 07:49:08

When to use Class.isInstance() & when to use instanceof operator?的相关文章

isInstance、isAssignableFrom与instanceof 的使用与区别

isInstance的参数为对象.例:调用者.getClass().isInstance(调用者本身或者父类接口的实例[object除外])返回true,反之false isAssignableFrom的参数为class,例:调用者.getClass().isAssignableFrom(调用者本身或者子类的class)返回true,反之false

Java反序列化漏洞通用利用分析

2015年11月6日,FoxGlove Security安全团队的@breenmachine 发布的一篇博客[3]中介绍了如何利用Java反序列化漏洞,来攻击最新版的WebLogic.WebSphere.JBoss.Jenkins.OpenNMS这些大名鼎鼎的Java应用,实现远程代码执行. 然而事实上,博客作者并不是漏洞发现者.博客中提到,早在2015年的1月28号,Gabriel Lawrence (@gebl)和Chris Frohoff (@frohoff)在AppSecCali上给出了

Akka框架使用注意点

1.mailbox Akka的每个actor默认有一个mailbox,按照FIFO顺序单线程处理.在抛出异常导致父actor根据设置的监管策略执行重启或恢复操作时,会从触发异常的消息的后续消息开始处理,邮箱并不会被清空.如果你想重新处理那个触发异常的消息,可以通过重写preRestart方法来访问该消息,java 中的preRestart参数为(Throwable reason, Option<Object> message),message.get()可以获得该消息(因为是从Option对象

[zz]Java中的instanceof关键字

1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given

虚拟机类加载机制(2)——类加载器

<深入理解Java虚拟机>一书中将类的加载过程放到了类加载器前面一节,但在这里我想先讲“类加载器”.在上一篇类加载时机中我们用大量篇幅来讲解了类加载过程中的5个步骤的最后一步——初始化.在这一节中,我们实际是在讲解类加载过程5个步骤的第一步——加载. 我们再次回顾类加载过程的5个步骤: 类加载过程的“加载”步骤是通过类加载器(ClassLoader)来实现的.那么加载阶段做什么工作呢?“通过一个类的全限定名来获取描述此类的二进制字节流.”首先我们需要了解来自同一个Class文件的两个类是否一定

Class对象的isAssignableFrom方法

isAssignableFrom 在看一个开源代码时,在加载完某个Class对象后,经常会使用 java.lang.Class#isAssignableFrom 来校验下. 之前真没有注意过Class对象中还有个这样的方法,下面是是这个方法的定义: public native boolean isAssignableFrom(Class<?> cls); 他到底是干什么用的呢? 先来看下Java源码中的注释: java.lang.Classpublic boolean isAssignable

Thinking in Java---类型信息和java反射机制学习笔记

前面学习的多态给了我们一个很好的承诺:我们编写的代码只要与基类打交道,而不用为每一个新增加的子类写一份代码.但是这种思想在我们想要访问子类自己定义的方法时,就会有问题了.如下面的代码所示: class Base1{ void f(){ System.out.println("Base.f()"); } } class Sub extends Base1{ void f(){ System.out.println("Sub.f()"); } void g(){ Sys

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

给你一组json数据结构,你把它解析出来到项目中,你会怎么做? // data1 sample { "code" : "1", "msg" : "Success", "data" : { "userid1" : { "name" : "Zhangsan", "sex" : "male" } } } // da

isinstance()

isinstance() 用于判断一个对象是否是给定的类型