instanceof 嚴格來講是Java中的一個雙目運算符,用來測試一個對象是否爲一個類的實例,用法爲:html
boolean result = obj instanceof Class
其中 obj 爲一個對象,Class 表示一個類或者一個接口,當 obj 爲 Class 的對象,或者是其直接或間接子類,或者是其接口的實現類,結果result 都返回 true,不然返回false。java
注意:編譯器會檢查 obj 是否能轉換成右邊的class類型,若是不能轉換則直接報錯,若是不能肯定類型,則經過編譯,具體看運行時定。算法
int i = 0; System.out.println(i instanceof Integer);//編譯不經過 System.out.println(i instanceof Object);//編譯不經過
instanceof 運算符只能用做對象的判斷。數組
System.out.println(null instanceof Object);//false
關於 null 類型的描述在官方文檔:https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1 有一些介紹。通常咱們知道Java分爲兩種數據類型,一種是基本數據類型,有八個分別是 byte short int long float double char boolean,一種是引用類型,包括類,接口,數組等等。而Java中還有一種特殊的 null 類型,該類型沒有名字,因此不可能聲明爲 null 類型的變量或者轉換爲 null 類型,null 引用是 null 類型表達式惟一可能的值,null 引用也能夠轉換爲任意引用類型。咱們不須要對 null 類型有多深入的瞭解,咱們只須要知道 null 是能夠成爲任意引用類型的特殊符號。oracle
在 JavaSE規範 中對 instanceof 運算符的規定就是:若是 obj 爲 null,那麼將返回 false。dom
Integer integer = new Integer(1); System.out.println(integer instanceof Integer);//true
這沒什麼好說的,最廣泛的一種用法。jvm
瞭解Java 集合的,咱們知道集合中有個上層接口 List,其有個典型實現類 ArrayList性能
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
因此咱們能夠用 instanceof 運算符判斷 某個對象是不是 List 接口的實現類,若是是返回 true,不然返回 false測試
ArrayList arrayList = new ArrayList(); System.out.println(arrayList instanceof List);//true
或者反過來也是返回 truespa
List list = new ArrayList(); System.out.println(list instanceof ArrayList);//true
咱們新建一個父類 Person.class,而後在建立它的一個子類 Man.class
public class Person { }
Man.class
public class Man extends Person{ }
測試:
Person p1 = new Person(); Person p2 = new Man(); Man m1 = new Man(); System.out.println(p1 instanceof Man);//false System.out.println(p2 instanceof Man);//true System.out.println(m1 instanceof Man);//true
注意第一種狀況, p1 instanceof Man ,Man 是 Person 的子類,Person 不是 Man 的子類,因此返回結果爲 false。
前面咱們說過編譯器會檢查 obj 是否能轉換成右邊的class類型,若是不能轉換則直接報錯,若是不能肯定類型,則經過編譯,具體看運行時定。
看以下幾個例子:
Person p1 = new Person(); System.out.println(p1 instanceof String);//編譯報錯 System.out.println(p1 instanceof List);//false System.out.println(p1 instanceof List<?>);//false System.out.println(p1 instanceof List<Person>);//編譯報錯
按照咱們上面的說法,這裏就存在問題了,Person 的對象 p1 很明顯不能轉換爲 String 對象,那麼天然 Person 的對象 p1 instanceof String 不能經過編譯,但爲何 p1 instanceof List 卻能經過編譯呢?而 instanceof List<Person> 又不能經過編譯了?
咱們能夠看Java語言規範Java SE 8 版:https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.20.2
若是用僞代碼描述:
boolean result; if (obj == null) { result = false; } else { try { T temp = (T) obj; // checkcast result = true; } catch (ClassCastException e) { result = false; } }
也就是說有表達式 obj instanceof T,instanceof 運算符的 obj 操做數的類型必須是引用類型或空類型; 不然,會發生編譯時錯誤。
若是 obj 強制轉換爲 T 時發生編譯錯誤,則關係表達式的 instanceof 一樣會產生編譯時錯誤。 在這種狀況下,表達式實例的結果永遠爲false。
在運行時,若是 T 的值不爲null,而且 obj 能夠轉換爲 T 而不引起ClassCastException,則instanceof運算符的結果爲true。 不然結果是錯誤的
簡單來講就是:若是 obj 不爲 null 而且 (T) obj 不拋 ClassCastException 異常則該表達式值爲 true ,不然值爲 false 。
因此對於上面提出的問題就很好理解了,爲何 p1 instanceof String 編譯報錯,由於(String)p1 是不能經過編譯的,而 (List)p1 能夠經過編譯。
JavaSE 8 instanceof 的實現算法:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.instanceof
一、obj若是爲null,則返回false;不然設S爲obj的類型對象,剩下的問題就是檢查S是否爲T的子類型;
二、若是S == T,則返回true;
三、接下來分爲3種狀況,之因此要分狀況是由於instanceof要作的是「子類型檢查」,而Java語言的類型系統裏數組類型、接口類型與普通類類型三者的子類型規定都不同,必須分開來討論。
①、S是數組類型:若是 T 是一個類類型,那麼T必須是Object;若是 T 是接口類型,那麼 T 必須是由數組實現的接口之一;
②、接口類型:對接口類型的 instanceof 就直接遍歷S裏記錄的它所實現的接口,看有沒有跟T一致的;
③、類類型:對類類型的 instanceof 則是遍歷S的super鏈(繼承鏈)一直到Object,看有沒有跟T一致的。遍歷類的super鏈意味着這個算法的性能會受類的繼承深度的影響。
參考連接:https://www.zhihu.com/question/21574535