Click to enter _ more _java thousand ask
1. What is a generic type
Generics are a new feature of Java SE 1.5, where generics are parameterized types, meaning that the data type being manipulated is specified as a parameter.
This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.
How to use generic methods, generic classes/interfaces See here: how generics work
In the absence of generics, the "arbitrariness" of the arguments by reference to the type object, the disadvantage of "arbitrariness" is the explicit coercion of type conversions, which require the developer to be able to predict the actual parameter type.
In the case of coercion of type conversion errors, the compiler may not prompt for an error and an exception occurs at run time, which is a security risk.
Therefore, the benefits of introducing generics into the Java language are safe and simple, checking for type safety at compile time, and all casts are automatic and implicit to improve the rate of reuse of code.
2, generics have those characteristics
- Generic type parameters can only be class types (including custom classes) and cannot be underlying types (such as int,double and Char).
- The same generic can correspond to multiple versions (because the parameter types are indeterminate), and different versions of the generic class instances are incompatible.
- The generic type of a class can have more than one.
- There is no inheritance relationship between generics, even if string inherits object. The following code is illegal
List ls = new ArrayList ();
List lo = ls;
- A generic type parameter can use the extends statement, for example. It is customary to call the bounded type.
- The type parameter of a generic can also be a wildcard type. such as Class
Java Technology _java thousand questions (0040) _ What is generics