1. 程式人生 > >我又不是你的誰--java instanceof操作符用法揭祕

我又不是你的誰--java instanceof操作符用法揭祕

背景故事

《曾經最美》是朱銘捷演唱的一首歌曲,由陳佳明填詞,葉良俊譜曲,是電視劇《水晶之戀》的主題曲。歌曲時長4分28秒。 歌曲歌詞:

  • 看不穿你的眼睛
  • 藏有多少悲和喜
  • 像冰雪細膩又如此透明
  • 彷彿片刻就要老去
  • 整個城市的孤寂
  • 不止一個你
  • 只能遠遠的
  • 想像慰藉我們之間的距離
  • 我又不是你的誰
  • 不能帶給你安慰
  • 忍心你枯萎凋零的玫瑰
  • 彷彿希望化成灰
  • 要不是痛徹心扉
  • 誰又記得誰
  • 只是雲和月
  • 相互以為是彼此的盈缺
  • 不能哭喊已破碎
  • 曾經的最美
  • 獨自一個人熟悉的街
  • 別問你在想誰
  • 不去追悔已憔悴
  • 愛過的機會
  • 真實已粉碎人事已非
  • 還有什麼最可貴
  • 我又不是你的誰
  • 不能帶給你安慰
  • 忍心你枯萎凋零的玫瑰
  • 彷彿希望化成灰
  • 要不是痛徹心扉
  • 誰又記得誰
  • 只是雲和月
  • 相互以為是彼此的盈缺
  • 不能哭喊已破碎
  • 曾經的最美
  • 獨自一個人熟悉的街
  • 別問你在想誰
  • 不去追悔已粉碎
  • 愛過的機會
  • 真實已粉碎人事已非
  • 還有什麼最可貴
  • 不能哭喊已破碎
  • 曾經的最美
  • 獨自一個人熟悉的街
  • 別問你在想誰
  • 不去追悔已憔悴
  • 愛過的機會
  • 真實已粉碎人事已非
  • 還有什麼最可貴

牽線之牛刀小試

如何判斷是不是誰的誰?java有一個instanceof操作符(關係操作符)可以做這件事。

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

打印出結果true

可是如果你的哪個誰不存在呢?請看程式碼:

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

很多人都會異口同聲的說

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會如何?

class Point { int x, y; }
class Element { int atomicNumber; }

public class InstanceofTest {
    public static void main(String[] args) {
 Point p = new Point();
 Element e = new Element();
 if (e instanceof Point) { 
 System.out.println("匹配成功!");
 }else {
     System.out.println("匹配不成功");
 }
    }
}

不少人會說:“匹配不成功”

 

抱歉,你又掉進坑裡了,這個會報編譯錯誤

 

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也會是編譯錯誤

class Point { int x, y; }
class Element { int atomicNumber; }
public class InstanceofTest {
    public static void main(String[] args) {
 Point p = new Point();
 Element e = new Element();
 p = (Point)e; // compile-time error
    }
}

牽線之暗藏玄機

編譯器並不是萬能的,並不能檢測出所有問題,看下面:

class Point { int x, y; }
class Element { int atomicNumber; }
public class InstanceofTest {
    public static void main(String[] args) {
 Point p = new Point();
 //Element e = new Element();
 p = (Point) new Object();
 System.out.println(p instanceof Point);
    }
}

猛一看,沒事問題,編譯也沒有問題,可是執行時報錯

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:

public boolean 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.

參考資料

【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-oper