Java is designed with a single root structure, except for the benefits of GC, in generic programming (template C + +), because all types are inherited from object, so using up-molded, we can write the following code:
public class Generictest {
public static void Main (string[] args) {
List List = new ArrayList ();
List.add ("Qqyumidi");
List.add ("corn");
List.add (100);
for (int i = 0; i < list.size (); i++) {
String name = (string) list.get (i); exception
System.out.println ("Name:" + name);
}
}
}
As you can see, we can put both the string and the int in the list without the template declaration, but the problem is that if you turn int into a string, it is not possible to use exception to handle this situation.
Java SE5 also introduces the concept of parameterized types, namely generics:
list<string> list = new arraylist<string> ();
List.add ("Qqyumidi");
List.add ("corn");
List.add (100); Hint compilation error
This is the generics that both follow, but Java's generic implementations are different from C + +:
public class Generictest {
public static void Main (string[] args) {
box<string> name = new Box<string> ("corn");
box<integer> age = new box<integer> (712);
System.out.println ("Name class:" + Name.getclass ()); Com.qqyumidi.Box
System.out.println ("Age class:" + Age.getclass ()); Com.qqyumidi.Box
System.out.println (name.getclass () = = Age.getclass ()); True
}
}
As you can see, at run time, different generics are actually using a type, and the same meaning of code in C + + is completely separate:
The idea behind generics in Java is that it only works in the code-compilation phase, where the generic information is erased when the generic result is correctly validated during compilation, meaning that the class file that was successfully compiled does not contain any generic information. Generic information is not entered into the run-time phase.
The raw type is the true type of the type variable that erased the generic information and finally the byte code. Whenever a generic type is defined, the corresponding primitive type is automatically provided. The type variable is erased (crased) and replaced with its qualified type (an unqualified variable with object).
Java 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.}
The original type of Pair<T> is:
Java 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.}
How does the compiler handle generics?
Typically, a compiler handles generics in two ways:
1.Code Specialization. A new target code (bytecode or binary code) is generated when instantiating a generic class or generic method. For example, for a generic list, you might need to generate three target codes for string,integer,float.
2.Code sharing. Only one copy of the target code is generated for each generic class, and all instances of the generic class are mapped to this target code, and type checking and type conversion are performed when needed.
Templates (template) in C + + are typical code specialization implementations. The C + + compiler generates a copy of the execution code for each generic class instance. The integer list and string list in the execution code are two different types. This can lead to code bloat, but experienced C + + programmers have the skill to avoid code bloat.
Another disadvantage of Code specialization is that it is a waste of space in a reference type system because the elements in the reference type collection are essentially pointers. There is no need to generate a copy of the execution code for each type. This is the main reason why the Java compiler uses code sharing to handle generics.
The Java compiler creates a unique bytecode representation for each generic type by code sharing, and maps instances of that generic type to this unique bytecode representation. The mapping of multiple generic class instances to a unique bytecode representation is implemented by type Erasue.
Generics in the language