Learning Java generics generics erased bring unnatural

Learning Java generics generics erased bring unnatural


Type erasure is a Java implementation of generics. In order to ensure that before the introduction of generic byte code can run on the new version of the virtual machine without any modification of circumstances drinks, so generics are the compiler to achieve this level, which maintained a Java-compatible platform sex. Generic type information declared in the Java source code, will be erased in the compilation process, leaving only the form without type parameters.

After type erasure, contains the generic type of code into the code does not contain a generic type, the equivalent of a return to form before being introduced generics, Java virtual machine does not know the generic type when you run the bytecode The presence. Although the API required for reflection, containing information related to the generic type in Java byte code, but this information is not performed in the byte codes being used.

Compiler and a virtual machine that can distinguish between different types: for the compiler, List <String> and List <Integer> of different types; and of the virtual machine, which are both of the type of List. Types available at run time may be referred to particular types (reifiable type). May be embodied in Java type comprises a non-generic type, the actual type is unbounded for all wildcards parameterized type, primitive type, base type, the element type to be particular types of data types, and the type and fu are themselves may specific types of nested type. As String, List <?>, List, int, String [] and MyClass <?>. Inner specific types are available.

Although the type of implementation is compatible, but this compatibility for Java language widely used for such a very important, but it also brings limitations of generics in unnatural and use the design.

basic type

You can specify the type of parameter types do not include basic types, so there is no "MyClass <double>", only "MyClass <Double>". This is because when the erasing, Object value can not be stored directly in the double type.

Static type

You can not use static member class in the type of variables, such as:

public class MyClass<T>{
	public static T getValue(){}
}

</pre><p><span style="color:rgb(51,51,51); font-family:Arial; line-height:26px; font-size:18px">这是由于类型变量实际代表的类型要到创建对象时才会给出,而静态方法只要类加载之后就可以调用了。但可以这样
<pre name="code" class="java">public class MyClass{
	public static <T> T getValue(){}
}


 
 

or
<pre name="code" class="java">public class MyClass<T>{
	public static <M> M getValue(){}
}


 
 

 
 class Object 
  
 

List <String> and a List <Integer> type for the virtual machine is the same, are represented by the List interface. So you can not get a class object literal parameterized type of a similar "List <String> .class" form, but can only use List.class. There is no List <String> type, only the List type at run time.

instanceof check

In Java 6, you can use instanceof to check this, but does not actually include checking the generic part, but at compile time will produce two warning messages.

<pre name="code" class="java">ArrayList<Integer> myIntList=new ArrayList<Integer>();
System.out.println(myIntList instanceof ArrayList<Integer>);
System.out.println(myIntList instanceof ArrayList<String>);


 
 

 
 But in Java 7, in addition to the actual type is unbounded wildcard generics can not use the instanceof operator, such as "myIntList instanceof ArrayList <?>" Is legal, but "myIntList instanceof ArrayList <Integer>)" is illegal, not by the compiler. Since these two parameterized types are erased after the type ArrayList, if such behavior allows developers to easily misunderstood. 
Published 40 original articles · won praise 17 · views 120 000 +

Guess you like

Origin blog.csdn.net/rendawei636/article/details/45897979
Recommended