Generic, broad data type "
You can accept data of any type (class).
Generic definitions (classes and interfaces)
The type parameter must be a valid identifier, habitually using a single uppercase letter, typically K for the key, V for the value, E for an exception or error, and T for the data type in general sense.
Example: Class Point<tx, ty>
Generic variables
A generic class must indicate a specific type, that is, to a type parameter when it is instantiated.
Cases:
TX x;
TX y;
Generic instantiation
Point<integer, integer> p = new Point<integer, integer> ();
Type Erase
Point P = new Point ();
Generic methods
Class Utest {
Public <tx, ty> TX Print (tx x, TY y)
public class Point<tx, ty> {
Public TX Print (tx x, TY y) {
Generic inheritance
Public <TSB extends t1> t Getmax (t array[]) {
Generic wildcard character (?)
To avoid type erasure, you can use a wildcard character (?) :
public void print (point<?,? > P) {
? Extends t indicates that a generic type parameter can only be T and its subclasses, similar to generic inheritance when defining a generic class or generic method.
Use wildcard characters (?) Not only can you limit the upper limit of the type, but you can also limit the lower. Restrict the lower limit to use the Super keyword, such as <? Super tsb> indicates that the TSB and its parent can only be accepted.
Java-based generics