First, emphasize one point:
For beginners like me, be sure to stand in the perspective of virtual machines and compilers to analyze the various features of the Java language, generics are no exception. (I think this is a correct learning experience)
Writing this article originated in the recent learning of Java, one day on the road and a colleague in the discussion of what is the Java generics, found that some concepts are very vague. So I want to clarify it and record it.
A friend who wants to see can correct the mistake inside. Thank you so much.
1 about the Java generic mechanism
In the original Java version,
Java generics are implemented through inheritance, and I think the inheritance here is not the inheritance of the class but the inheritance of the type.
Example
public class Test
{
Object var;
Object GetVar ();
Void SetVar (Object);
}
This is a class.
This is what a generic application does.
Test testobj= new =test ();
Class A
{}
A = new A ();
A = (a) GetVar ();
Because the Class A is a subclass of the object class, the assignment can be implemented by casting the type.
Class B, Class C and so on.
This way, you achieve the same effect as a C + + generic. The only thing to be aware of is type conversions, and the types that determine these types of operations require programmer intervention.
I suspect that Java's generic mechanism is built on the basis of type inheritance, and later versions of generics essentially do the same.
2 about type erasure of generics
As a developer of a generic class, we define a generic class that
Like what
Example
public class Test<t>
{
T var;
T GetVar ();
Void SetVar (T t);
}
Note that this t is a type parameter relative to class Test, (You can analogy: in void func (int x), int x is a type parameter relative to Method:func, X is a type variable).
This generic class will be processed in the compiler to generate a class file.
Select the original type (such as the object class), overriding all T within the class definition,
Remove <T> Erase
The result is that the generated class file effect is equivalent to
public class Test
{
Object var;
Object GetVar ();
void SetVar (Object t);
}
This process is called Java generic type erasure.
What does this process explain?
Note that for compilers, there is no test<string> class,test<date> class, and so on.
For test<xx> various objects, they belong to the same class Test;
Note, however, that the compiler sees the generic format <t, and the compiler does some extra things, such as type conversions, bridge methods, and so on. The compiler is here to do what the previous programmer needs to do manually.
After all, the code written by the programmer is to be read by the compiler. With some generic convention rules, the programmer's intent is to know exactly what the compiler knows, so the code written will not be problematic.
3 inheritance issues for generic types
First, inheritance is a question of the relationship between a class and a type, so it has nothing to do with the object. There is no inheritance problem between objects.
In the above analysis, we know that the compiler does not produce classes with specific type parameters. Only a normal class of the original type is generated. That is, there is only one class.
So where do you get the inheritance of generic types?
My understanding is that the inheritance of types and the inheritance of classes is not the same concept, it should be said, type inheritance is Type inheritance, class inheritance is also type inheritance, but type inheritance is not necessarily class inheritance
Texample:test<string > is a subtype of Test, not: test<string > is a class, test is another class,test<string >< /c3> is a subclass of Test.
In the concept of inheritance, type inheritance is a superclass, and class inheritance is a subclass. , class inheritance is also a case of type inheritance, and it is not possible to say that because a type is inherited type B, it infers that a is a class and B is another class. A is a subclass of B.
I don't know if it's defined in UML.
Beginner Java 12 Generic programming personal Understanding Summary