Java generics (generics) is a new feature introduced in JDK 5, which provides a compile-time type-safe detection mechanism that allows programmers to detect illegal types at compile time. The essence of a generic is a parameterized type, which means that the data type being manipulated is specified as a parameter. Suppose we have a need to write a sorting method that can sort an array of integers, arrays of strings, and even any other type of array, how do we do that? The answer is that you can use Java generics. Using the concept of Java generics, we can write a generic method to sort an array of objects. The generic method is then called to sort an integer array, a floating-point array, an array of strings, and so on.
650) this.width=650; "src=" http://upload.lanqiao.cn/image/20170711/1499737190230371.jpg "title=" 1499737190230371. JPG "alt=" u=3645943253,1699441531&fm=26&gp=0.jpg "style=" margin:0px;padding:0px;border:0px; Vertical-align:middle; "/>
Generic methods
You can write a generic method that can receive different types of arguments when it is called. Depending on the type of argument passed to the generic method, the compiler handles each method call appropriately.
The following is the rule that defines a generic method:
All generic method declarations have one type parameter declaration part (delimited by angle brackets), which is part of the type parameter declaration before the method return type
each type parameter declaration section contains one or more type parameters, separated by commas between the parameters. A generic parameter, also known as a type variable, is the identifier used to specify a generic type name. a type parameter can be used to declare a placeholder for the actual parameter type that returns a value type and can be obtained as a generic method. the declaration of a generic method body is the same as other methods. Note the type parameter can only represent the reference type, not the original type (Int,double,char, and so on).
Java-based Java generics