Generic, which is the parameterized type. When referring to a parameter, it is most familiar to define the method when the physical parameter is called and then pass the argument when calling this method. So how do parameterized types understand? As the name implies, the type is parameterized by the original specific type, similar to the variable parameter in the method, when the type is also defined as a Parameter form (which can be called a type parameter), and then the specific type (type argument) is passed in when using/calling.
Custom generic interfaces, generic classes, and generic methods
Public classGenerictest { Public Static voidMain (string[] args) {Box<String> name =NewBox<string> ("Corn"); System.out.println ("Name:" +name.getdata ()); }}classBox<t> { PrivateT data; PublicBox () {} PublicBox (T data) { This. data =data; } PublicT GetData () {returndata; }}
As a result, we find that when using a generic class, although different generic arguments are passed in, but there is no real sense of generating different types, the generic classes passing in different generic arguments have only one in memory, that is, the original basic type (box in this case), of course, Logically, we can understand a number of different generic types.
The reason for this is that the concept of generics in Java is intended to be a function of the code compilation phase, during the compilation process, for the correct validation of generic results, the generic information will be erased, that is, the successful compilation of the class file does not contain any generic information. Generic information is not entered into the run-time phase.
This is summed up in a sentence: a generic type is logically viewed as multiple different types, and is actually the same basic type.
Windstep
java-generic "excerpt"