1. Introduction to Java Generics
Generics are a new feature of Java 1.5. The essence of generics is a parameterized type, which means that the data type being operated is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods respectively.
The advantage of Java generics being introduced is safety and simplicity.
Before Java SE 1.5, in the absence of generics, the "arbitrary" parameter was realized by referencing the type Object. The disadvantage of "arbitrary" was that explicit forced type conversion was required, and This conversion requires developers to know the actual parameter types beforehand. For forced type conversion errors, the compiler may not prompt an error, and an exception will occur during runtime. This is a security risk.
The advantage of generics is that type safety is checked during compilation, and all casts are automatic and implicit, improving code reuse.
There are some rules and restrictions in the use of generics:
1. The type parameters of generics can only be class types (including custom classes), not simple types.
2. The same generic type can correspond to multiple versions (because the parameter type is uncertain), and different versions of generic class instances are incompatible.
3. Generics can have multiple type parameters.
4. Generic parameter types can use the extends statement, for example. Customarily become "bounded types".
5. Generic parameter types can also be wildcard types.
Class<?> classType = Class.forName(java.lang.String);
Generics also include interfaces, methods, etc. There is a lot of content, and it takes a lot of effort to understand and apply it skillfully.
2. Java generics implementation principle: type erasure
Java’s generics are pseudo-generics. During compilation, all generic information is erased. The first prerequisite for correctly understanding the concept of generics is to understand type erasure.
Generics in Java are basically implemented at the compiler level. The type information in generics is not included in the generated Java bytecode. Type parameters added when using generics will be removed by the compiler during compilation. This process is called type erasure.
Types such as List
3. The original type retained after type erasure
The original type (raw type) is the real type of the type variable in the bytecode after the generic information has been erased. Whenever a generic type is defined, the corresponding primitive type is automatically provided. Type variables are erased (crased) and replaced with their qualified type (unqualified variables are Object).
class Pair<T> { private T value; public T getValue() { return value; } public void setValue(T value) { this.value = value; } }
The original type of Pair
class Pair { private Object value; public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } }
Because in Pair
If the type variable is bounded, then the original type is replaced by the first bounding type variable.
For example, if Pair declares like this:
public class Pair<T extends Comparable& Serializable> {
Then the original type is Comparable
Note:
If Pair declares public class Pair
To distinguish between primitive types and generic variable types
When calling a generic method, you can specify the generic type or not.
Without specifying a generic, the type of the generic variable is the lowest level of the same parent class of several types in the method, until Object.
When specifying a generic, several types in the method must be the generic instance type or its subclass.
public class Test{ public static void main(String[] args) { /**不指定泛型的时候*/ int i=Test.add(1, 2); //这两个参数都是Integer,所以T为Integer类型 Number f=Test.add(1, 1.2);//这两个参数一个是Integer,以风格是Float,所以取同一父类的最小级,为Number Object o=Test.add(1, "asd");//这两个参数一个是Integer,以风格是Float,所以取同一父类的最小级,为Object /**指定泛型的时候*/ int a=Test.<Integer>add(1, 2);//指定了Integer,所以只能为Integer类型或者其子类 int b=Test.<Integer>add(1, 2.2);//编译错误,指定了Integer,不能为Float Number c=Test.<Number>add(1, 2.2); //指定为Number,所以可以为Integer和Float } //这是一个简单的泛型方法 public static <T> T add(T x,T y){ return y; } }
In fact, in a generic class, when generics are not specified, it is almost the same, except that the generic type at this time is Object, just like in ArrayList, if generics are not specified, then any type can be placed in this ArrayList Object.
4. C++ template implementation
Although I don’t understand C++, I also looked for the implementation of C++ on the Internet.
The phenomenon of generating different types for each template instantiation in C++ is called "template code bloat".
For example, vector
For more articles related to the implementation principles of Java generics, please pay attention to the PHP Chinese website!