日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java instead of 用法_我又不是你的谁--java instanceof操作符用法揭秘

發布時間:2023/12/1 java 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java instead of 用法_我又不是你的谁--java instanceof操作符用法揭秘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景故事

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

看不穿你的眼睛

藏有多少悲和喜

像冰雪細膩又如此透明

仿佛片刻就要老去

整個城市的孤寂

不止一個你

只能遠遠的

想像慰藉我們之間的距離

我又不是你的誰

不能帶給你安慰

忍心你枯萎凋零的玫瑰

仿佛希望化成灰

要不是痛徹心扉

誰又記得誰

只是云和月

相互以為是彼此的盈缺

不能哭喊已破碎

曾經的最美

獨自一個人熟悉的街

別問你在想誰

不去追悔已憔悴

愛過的機會

真實已粉碎人事已非

還有什么最可貴

我又不是你的誰

不能帶給你安慰

忍心你枯萎凋零的玫瑰

仿佛希望化成灰

要不是痛徹心扉

誰又記得誰

只是云和月

相互以為是彼此的盈缺

不能哭喊已破碎

曾經的最美

獨自一個人熟悉的街

別問你在想誰

不去追悔已粉碎

愛過的機會

真實已粉碎人事已非

還有什么最可貴

不能哭喊已破碎

曾經的最美

獨自一個人熟悉的街

別問你在想誰

不去追悔已憔悴

愛過的機會

真實已粉碎人事已非

還有什么最可貴

牽線之牛刀小試

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

public static voidmain(String[] args) {

String s= "Hello World!";

System.out.println(sinstanceofString);

}

打印出結果true

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

public static voidmain(String[] args) {

String s= null;

System.out.println(sinstanceofString);

}

很多人都會異口同聲的說

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 { intx, y; }class Element { intatomicNumber; }public classInstanceofTest {public static voidmain(String[] args) {

Point p= newPoint();

Element e= newElement();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 { intx, y; }class Element { intatomicNumber; }public classInstanceofTest {public static voidmain(String[] args) {

Point p= newPoint();

Element e= newElement();

p= (Point)e; //compile-time error

}

}

牽線之暗藏玄機

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

class Point { intx, y; }class Element { intatomicNumber; }public classInstanceofTest {public static voidmain(String[] args) {

Point p= newPoint();//Element e = new Element();

p = (Point) newObject();

System.out.println(pinstanceofPoint);

}

}

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

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 booleanareObjectsAssignable(Object left, Object right) {returnleft.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-operator

總結

以上是生活随笔為你收集整理的Java instead of 用法_我又不是你的谁--java instanceof操作符用法揭秘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。