天天看點

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

背景故事

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

看不穿你的眼睛

藏有多少悲和喜

像冰雪細膩又如此透明

仿佛片刻就要老去

整個城市的孤寂

不止一個你

隻能遠遠的

想像慰藉我們之間的距離

我又不是你的誰

不能帶給你安慰

忍心你枯萎凋零的玫瑰

仿佛希望化成灰

要不是痛徹心扉

誰又記得誰

隻是雲和月

互相以為是彼此的盈缺

不能哭喊已破碎

曾經的最美

獨自一個人熟悉的街

别問你在想誰

不去追悔已憔悴

愛過的機會

真實已粉碎人事已非

還有什麼最可貴

不去追悔已粉碎

牽線之牛刀小試

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

列印出結果true

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

很多人都會異口同聲的說

false

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

你答對了。

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

不少人會說:“比對不成功”

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

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

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

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

牽線之暗藏玄機

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

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

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:

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