The principle Java generics (rpm)

Since some time ago I found that some of the code repetitive great company, you can use a generic method simplified, proposed to the leadership, leadership let me order a bit about generics only to others.

A, Java generics introduced

 

    Generic Java 1.5 new features is the essence of the generic type parameter, that is to say the operation of the data type is specified as a parameter. This parameter type may be used in the creation of classes, interfaces and methods, generic class, generic interfaces, generic methods are called.

 

       The benefits of Java generics introduced is safe and simple.

 

    Before Java SE 1.5, without the generic case, the parameters achieved by reference to Object type "arbitrary" and "arbitrary" of the disadvantages of explicit do is cast, and this conversion is required for the developer can predict the actual parameter type performed. For mandatory conversion error, the compiler may not prompt an error, it appears abnormal at run time, this is a security risk.

 

    The benefits of generics are checked at compile time type safety, and all the cast are automatic and implicit, improve code reuse.

 

    Generics in use, there are some rules and restrictions:

 

1, only a generic type parameter class types (including a custom class), can not be a simple type.

 

2, the same may correspond to a plurality of generic versions (because the parameter type is uncertain), different versions of the generic class instance are not compatible.

 

3, the generic type parameter can have multiple.

 

4, the generic type parameter extends statement can be used, for example. Become accustomed to the "bounded type."

5, the generic parameter type may also be wildcard type.

 

Java code   Collection Code
  1. Class<?> classType = Class.forName(java.lang.String);  

    There are generic interfaces, methods, and so on, a lot of content, it takes a lot of effort to understand and master the skilled application.

Two, Java implementation of generics principle: the type of clashes

       Java generics is a pseudo-generics. During compilation, all generic information will be erased. Most important prerequisite for proper understanding of the generic concept is to understand the type of clashes (type erasure).

       Java generics are basically the compiler to achieve this level. In Java bytecode is not generated in the type of information included in the generic. When coupled with the use of the generic type parameter, will be removed at compile time the compiler. This process is called type erasure.

    List code as defined in the <object> and List <String> and other types, will be compiled in a program List. JVM only see the List, but by the generic type of additional information on the JVM is not visible. Java compiler will discover what could be wrong as far as possible at compile time, but still can not avoid the type of conversion exceptions at run time. An important difference between the generic type erasure is Java implementation of C ++ template mechanism implementation (described later).

Third, the retention of the original type type erasure

    Primitive types (raw type) is erased to generic information, and finally the actual type of the variable's type in byte code. Whenever you define a generic type and a corresponding original type will be automatically provided. Type variable erased (crased), which defines the type and use (indefinite variables Object) replaced.

Java code   Collection Code
  1. class Pair<T> {    
  2.   private T value;    
  3.   public T getValue() {    
  4.     return value;    
  5.   }    
  6.   public void setValue(T  value) {    
  7.     this.value = value;    
  8.   }    
  9. }    

 Pair <T> primitive types:

Java code   Collection Code
  1. class Pair {    
  2.   private Object value;    
  3.   public Object getValue() {    
  4.     return value;    
  5.   }    
  6.   public void setValue(Object  value) {    
  7.     this.value = value;    
  8.   }    
  9. }  

    Because Pair <T>, T is a type of indefinite variables, replaced with Object. The result is an ordinary class, as generic as before adding java language has become realized. Programs may contain different types of Pair, such as Pair <String> or Pair <Integer>, however, they become after erasing the original type Pair type of primitive types are Object.

 

If there is a variable type defined, the original type can be replaced by a first variable type boundary.

For example, Pair this statement:

Java code   Collection Code
  1. public class Pair<T extends Comparable& Serializable> {   

    So is the original type Comparable

 

    note:

    If this statement Pair public class Pair <T extends Serializable & Comparable>, then replaces the original type with Serializable, and the compiler when necessary Comparable To insert cast. To improve efficiency, the label should be (Tagging) interfaces (i.e., without an interface method) at the end of the boundary defined list.

    To distinguish between the original and generic type variable type

    When calling a generic method, you can specify generic may not specify generic.

    In the case of generic not specified, the type of generic variables to a minimum level the same parent class types of the process until the Object.

    Generic specified time, the process must be several types of the generic type or a subclass instance.

Java code   Collection Code
  1. public class Test{    
  2.   public static void main(String[] args) {    
  3.     / ** * When you do not specify the generic /    
  4.     int i = Test.add (1, 2); // Both parameters are Integer, so the type Integer T    
  5.     Number f = Test.add (1, 1.2); // this is a two parameter Integer, style is to Float, the minimum level so take the same parent class to Number    
  6.     Object o = Test.add (1, "asd"); // this is a two parameter Integer, style is to Float, the minimum level so take the same parent class the Object    
  7.     
  8.     / ** * Specifies the time the generic /    
  9.     int a = Test <Integer> add (1, 2);. // specified Integer, so only type Integer or a subclass    
  10.     int b = Test <Integer> add (1, 2.2);. // compile error, specified Integer, Float is not    
  11.     Number c = Test <Number> add (1, 2.2);. // specified to Number, it is possible to Integer and Float    
  12.   }    
  13.   
  14.   // This is a simple generic method    
  15.   public static <T> T add(T x,T y){    
  16.     return y;    
  17.   }    
  18. }    

    In fact, generic classes, generic is not specified when, almost, but this time as a generic type Object, such as ArrayList on, if you do not specify the generic, then the ArrayList can put any type of object.

Four, C ++ templates to achieve

    Although I do not know C ++, but I also find online the next C ++ implementation.

    In c ++ for each instance of the template to produce a different type, this phenomenon is called "template code bloat."

Such as vector <int>, vector <char>, vector <double>, this will generate a total of 3 different parts of the code vector.

Guess you like

Origin www.cnblogs.com/ffaiss/p/11460593.html