背景故事

《曾经最美》是朱铭捷演唱的一首歌曲,由陈佳明填词,叶良俊谱曲,是电视剧《水晶之恋》的主题曲。歌曲时长4分28秒。 歌曲歌词:

  • 看不穿你的眼睛
  • 藏有多少悲和喜
  • 像冰雪细腻又如此透明
  • 仿佛片刻就要老去
  • 整个城市的孤寂
  • 不止一个你
  • 只能远远的
  • 想像慰藉我们之间的距离
  • 我又不是你的谁
  • 不能带给你安慰
  • 忍心你枯萎凋零的玫瑰
  • 仿佛希望化成灰
  • 要不是痛彻心扉
  • 谁又记得谁
  • 只是云和月
  • 相互以为是彼此的盈缺
  • 不能哭喊已破碎
  • 曾经的最美
  • 独自一个人熟悉的街
  • 别问你在想谁
  • 不去追悔已憔悴
  • 爱过的机会
  • 真实已粉碎人事已非
  • 还有什么最可贵
  • 我又不是你的谁
  • 不能带给你安慰
  • 忍心你枯萎凋零的玫瑰
  • 仿佛希望化成灰
  • 要不是痛彻心扉
  • 谁又记得谁
  • 只是云和月
  • 相互以为是彼此的盈缺
  • 不能哭喊已破碎
  • 曾经的最美
  • 独自一个人熟悉的街
  • 别问你在想谁
  • 不去追悔已粉碎
  • 爱过的机会
  • 真实已粉碎人事已非
  • 还有什么最可贵
  • 不能哭喊已破碎
  • 曾经的最美
  • 独自一个人熟悉的街
  • 别问你在想谁
  • 不去追悔已憔悴
  • 爱过的机会
  • 真实已粉碎人事已非
  • 还有什么最可贵

牵线之牛刀小试

如何判断是不是谁的谁?java有一个instanceof操作符(关系操作符)可以做这件事。

  1. public static void main(String[] args) {
  2. String s = "Hello World!";
  3. System.out.println(s instanceof String);
  4. }

打印出结果true

可是如果你的哪个谁不存在呢?请看代码:

  1. public static void main(String[] args) {
  2. String s = null;
  3. System.out.println(s instanceof String);
  4. }

很多人都会异口同声的说

false

你答对了。

JSL-15.20.2规定

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

牵线之乱点鸳鸯谱

如果没有任何关系的两个类使用instanceof会如何?

  1. class Point { int x, y; }
  2. class Element { int atomicNumber; }
  3.  
  4. public class InstanceofTest {
  5. public static void main(String[] args) {
  6. Point p = new Point();
  7. Element e = new Element();
  8. if (e instanceof Point) {
  9. System.out.println("匹配成功!");
  10. }else {
  11. System.out.println("匹配不成功");
  12. }
  13. }
  14. }

不少人会说:“匹配不成功”

抱歉,你又掉进坑里了,这个会报编译错误

JSL-15.20.2规定

The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type, or a compile-time error occurs.

It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error (§15.16), then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.

当然,cast也会是编译错误

  1. class Point { int x, y; }
  2. class Element { int atomicNumber; }
  3. public class InstanceofTest {
  4. public static void main(String[] args) {
  5. Point p = new Point();
  6. Element e = new Element();
  7. p = (Point)e; // compile-time error
  8. }
  9. }

牵线之暗藏玄机

编译器并不是万能的,并不能检测出所有问题,看下面:

  1. class Point { int x, y; }
  2. class Element { int atomicNumber; }
  3. public class InstanceofTest {
  4. public static void main(String[] args) {
  5. Point p = new Point();
  6. //Element e = new Element();
  7. p = (Point) new Object();
  8. System.out.println(p instanceof Point);
  9. }
  10. }
  1. 猛一看,没事问题,编译也没有问题,可是运行时报错

Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to Point

上面的程序展示了当要被转型的表达式的静态类型是转型类型的超类时,转型操作符的行为。与instanceof 操作相同,如果在一个转型操作中的两种类型都是类,那么其中一个必须是另一个的子类型。尽管对我们来说,这个转

型很显然会失败,但是类型系统还没有强大到能够洞悉表达式new Object()的运行期类型不可能是Point的一个子类型。因此,该程序将在运行期抛出ClassCastException 异常。

牵线之竞争激烈

关系操作符instanceof可不是市场上唯一的选择,另外一个背靠大山的家伙要注意了

Class 的方法

booleanisInstance(Object obj)

Determines if the specified Object is assignment-compatible with the object represented by this Class.

那么什么时候该用instanceof 什么时候该用isInstance呢

我的理解是

instanceof偏向于比较class之间

isInstance偏向于比较instance和class之间

stackoverflow也有此问题的解答:

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:

  1. public boolean areObjectsAssignable(Object left, Object right) {
  2. return left.getClass().isInstance(right);
  3. }

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.

参考资料

【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.20.2

【2】java解惑

【3】https://stackoverflow.com/questions/8692214/when-to-use-class-isinstance-when-to-use-instanceof-operator

我又不是你的谁--java instanceof操作符用法揭秘的更多相关文章

  1. Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法

    ylbtech-Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法 1.返回顶部 1. Java 实例 - instanceof 关键字用法   ...

  2. Java 实例 - instanceof 关键字用法

    Java 实例 - instanceof 关键字用法 instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符. instanceof 是 Java 的保留关键 ...

  3. C++ 'dynamic_cast' and Java 'instanceof' 使用对比

    在Java中,如果A是基类,B是A的派生类,那么instanceof可以用来判断一个实例对象是A还是B,相当于一个二元操作符,但与==, >, < 不同的是,它是由字母组成,是Java的保 ...

  4. Java Instanceof

    Java Instanceof Instanceof是一个非常简单的运算符,前一个操作通常是一个引用类型的变量,后一个操作数通常是一个类(也可以是接口,可以把接口理解成一种特殊的类),它用于判断前面的 ...

  5. [Java学习] Java instanceof 运算符

    多态性带来了一个问题,就是如何判断一个变量所实际引用的对象的类型 . C++使用runtime-type information(RTTI),Java 使用 instanceof 操作符. insta ...

  6. java基础-操作符

    浏览以下内容前,请点击并阅读 声明 定义:操作符是一种能够对一个,两个或三个操作对象执行特定的操作,并返回一个结果的特定符号. java中操作符的种类和优先级如下表所示,除了赋值运算,所有二元操作符运 ...

  7. 设计模式 - 观察者模式(Observer Pattern) Java内置 用法

    观察者模式(Observer Pattern) Java内置 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659 ...

  8. 使用instanceof操作符判断对象类型及方法的重载

    学习内容: 一.使用instanceof操作符判断对象类型 1.instanceof操作符可以判断一个实例对象是否属于一个类. 语法:对象名 instanceof 类名 2.使用instanceof表 ...

  9. JAVA的continue用法

    JAVA的continue用法: public class test{ public static void main(String [] args){  for(int i=0;i<=10;i ...

随机推荐

  1. BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分+单点更新+区间求和+区间求最大值)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:略. 题解:树链剖分模版,注意一些细节即可. #include <ios ...

  2. CF989C A Mist of Florescence 构造 思维好题 第八题

    A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. SpringMVC的工作原理图

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  4. SRAM和DRAM的区别

    一.浅谈关于SRAM和DRAM的区别:https://www.cnblogs.com/nano94/p/4014082.html. 二.ROM.RAM.DRAM.SRAM和FLASH的区别,转自:ht ...

  5. JWT与Session的比较

    如今,越来越多的项目开始采用JWT作为认证授权机制,那么它和之前的Session究竟有什么区别呢?今天就让我们来了解一下. JWT是什么 定义 JSON Web Token(JWT)是一个开放标准(R ...

  6. AutoCompleteTextView自动完成文本框

    AutoCompleteTextView是从EditText派生出来的,比普通编辑框多了一个功能,当用户输入一定字符后,自动完成文本框会显示一个下拉单,供用户选择,当选中一个后,被选中的内容会显示在文 ...

  7. JAVA测试(选择题)

    1.分析如下所示的Java代码,其中this关键字的意思是(  ) Public class Test { Private String name; Public String getName(){ ...

  8. NTP服务器实现

    时间服务器是一种计算机网络仪器,它从参考时钟获取实际时间,再利用计算机网络把时间信息传递给用户.虽然还有一些比较少用或过时的协议仍然在使用,但现时最重要及广泛使用,作为时间信息发送和同步化的协议是网络 ...

  9. C#中 CS1752无法嵌入互操作类型"OPCServerClass"。请改用适用的接口。

    使用C#+VS开发OPC程序是,调用Interop.OPCAutomation中的类时,提示无法嵌入互操作类型"OPCServerClass".请改用适用的接口. 首先说一下它的含 ...

  10. ES6新增常见特性

    一:声明属性let const var let const 区别 1.var声明变量会发生变量提升,let.const不会发生变量提升 2.var允许重复声明变量,let不可以 3.const声明变量 ...