type erasure & reified generic
Java的泛型不同于C++的模板:Java泛型是"type erasure",C++模板是"reified generic"。
- type erasure:泛型類型僅存在于編譯期間,編譯后的字節(jié)碼和運(yùn)行時(shí)不包含泛型信息,所有的泛型類型映射到同一份字節(jié)碼。
- reified generic:泛型類型存在于編譯和運(yùn)行期間,編譯器自動(dòng)為每一種泛型類型生成類型代碼并編譯進(jìn)二進(jìn)制碼中。
為什么Java是type erasure
這是由于泛型是后來(SE5)才加入到Java語言特性的,Java讓編譯器擦除掉關(guān)于泛型類型的信息,這樣使得Java可以向后兼容之前沒有使用泛型的類庫(kù)和代碼,因?yàn)樵谧止?jié)碼層面是沒有泛型概念的。
type erasure的本質(zhì)
泛型(T) --> 編譯器(type erasure) --> 原始類型(T被Object替換)
泛型(? extends XXX) --> 編譯器(type erasure) --> 原始類型(T被XXX替換)
原始類型指被編譯器擦除了泛型信息后,類型變量在字節(jié)碼中的具體類型。
假如,我們定義一個(gè)泛型類Generic是這樣的:
class Generic<T> {
private T obj;
public Generic(T o) {
obj = o;
}
public T getObj() {
return obj;
}
}
那么,Java編譯后的字節(jié)碼中Generic相當(dāng)于這樣的:
class Generic {
private Object obj;
public Generic(Object o) {
obj = o;
}
public Object getObj() {
return obj;
}
}
假如,我們使用Generic類是這樣的:
public static void main(String[] args) {
Generic<String> generic = new Generic<String>("hehe...");
String str = generic.getObj();
}
那么,Java編譯后的字節(jié)碼中相當(dāng)于這樣的:
public static void main(String[] args) {
Generic generic = new Generic("hehe...");
String str = (String) generic.getObj();
}
所以,所有Generic的泛型類型實(shí)質(zhì)是同一個(gè)類:
public static void main(String[] args) {
Generic<Integer> a = new Generic<Integer>(111);
Generic<String> b = new Generic<String>("bbb");
System.out.println("a'class: " + a.getClass().getName());
System.out.println("b'class: " + b.getClass().getName());
System.out.println("G'class: " + Generic.class.getName());
System.out.println("a'class == b'class == G'class: " + (a.getClass() == b.getClass() && b.getClass() == Generic.class));
}
上述代碼執(zhí)行結(jié)果:
a'class: generic.Generic
b'class: generic.Generic
G'class: generic.Generic
a'class == b'class == G'class: true
小結(jié):Java的泛型只存在于編譯時(shí)期,泛型使編譯器可以在編譯期間對(duì)類型進(jìn)行檢查以提高類型安全,減少運(yùn)行時(shí)由于對(duì)象類型不匹配引發(fā)的異常。
type erasure導(dǎo)致泛型的局限性
類型擦除降低了泛型的泛化性,使得某些重要的上下文環(huán)境中不能使用泛型類型,具有一定的局限性。
運(yùn)行時(shí)隱含類型轉(zhuǎn)換的開銷
使用泛型時(shí),Java編譯器自動(dòng)幫我們生成了類型轉(zhuǎn)換的代碼,這相對(duì)于C++模板來說無疑帶來了額外的性能開銷。
類型參數(shù)不能實(shí)例化
T obj = new T(); // compile error
T[] objs = new T[10]; // compile error
Generic<String> generic = new Generic<String>[10]; // compile error
類型參數(shù)不能進(jìn)行類型查詢(類型查詢?cè)谶\(yùn)行時(shí),運(yùn)行時(shí)類型參數(shù)已被擦除)
Generic<Integer> a = new Generic<Integer>(111);
if(a instanceof Generic<String>)// compile error
if(a instanceof Generic<T>) // compile error
if(a instanceof Generic) // 僅測(cè)試了a是否是Generic,忽略了類型參數(shù)
不能在靜態(tài)域和靜態(tài)方法中引用類型變量
class Generic<T> {
private static T obj;// compile error
public static T func(){...}// compile error
}
因?yàn)樗蟹盒皖愖罱K映射到同一個(gè)原始類型類,而靜態(tài)屬性是類級(jí)別的,類和實(shí)例共同擁有它的一份存儲(chǔ),因此一份存儲(chǔ)無法安放多個(gè)類型的屬性。靜態(tài)方法也是如此。
重載方法簽名沖突:
public boolean equals(T obj) // compile error
public boolean equals(T obj)被擦除類型后變?yōu)?code>public boolean equals(Object obj),與根類Object的public boolean equals(Object obj)簽名一樣,而兩者均不能覆蓋對(duì)方,導(dǎo)致編譯期名稱沖突。
一個(gè)類不能實(shí)現(xiàn)同一個(gè)泛型接口的兩種變體:
interface IFace<T>() {}
class FaceImpParent implements IFace<String> {}
class FaceImpChild extends FaceImpParent implements IFace<Integer> {} // compile error
原因是IFace<String>和IFace<Integer>在擦除類型后是同一個(gè)接口,一個(gè)類不能實(shí)現(xiàn)兩次同一個(gè)接口。
泛型類不能擴(kuò)展java.lang.Throwable
class GenericException <T> extends Exception {} // compile error