1. Overview
Generics enable types (classes and interfaces) to is parameters when defining classes, interfaces and methods.
Generics can use types (classes and interfaces) as parameters when defining classes, interfaces, and methods. are similar to the formal parameters of a method declaration in order to reuse the same code at different inputs. The difference is that the formal parameter input is the value and the generic is the type .
2. Why generics are used
1. More robust (stronger) type detection at compile time
The Java compiler makes strong-type detection of generic code and emits a type error once it detects that the code violates type safety.
Fixing compilation errors is easier than running errors, because it is difficult to find the problem with a run-time error.
2. No type conversion
No generics are usedListList=New ArrayList ();List. Add ("Without generics");//need to force type conversion string S1 = (string) list.get ( 0); //using generics list<string> list2 = New Arraylist<string> (); List2.add ( "generics"); string S2 = list2.get (0); //do not need to convert
3. Enable programmers to implement algorithms that are more commonly used
By using generics, which enable programmers to implement universal algorithms, algorithms will be able to be used in different types, customizable, type-safe, and easy-to-read.
If you write only one sort method, you can sort arrays of shapes, arrays of strings, and even any type of array that supports sorting.
3. Specific use 1. Generics (generic type)
A generic type is a generic class or interface that's parameterized over types.
Generics are a generic class or interface that is parameterized by type.
The format definition for the following polygons of a generic class:
class name<T1, T2, ..., Tn> { /* ... */ }
Learn by using the non-generic and generic versions of the box class:
Non-generic:
//non-generic classpublic class Box { private Object object; public Object getObject() { return object; } public void setObject(Object object) { this.object = object; }}
Generic type:
//generic classpublic class Box<T> { //T表示Type private T t; public T getT() { return t; } public void setT(T t) { this.t = t; }}
Type parameter naming conventions by convention, the name of the type parameter is made up of an uppercase letter.
E-element element (used extensively by the Java collections Framework)
K-key Key
N-number value
T-type type
V-value value
S,u,v etc.-2nd, 3rd, 4th types Second, third, fourth type
Invoking and instantiating generic classes
To obtain a reference to a generic class, you must perform a call to the generic class, using a specific value instead of T, such as Integer.
//获得引用Box<Integer> integerBox;//实例化integerBox = new Box<Integer>();
A call to a generic class is similar to a normal method call, except that the generic class passes the type parameter (Typeargument) and the method call passes the parameter (argument).
Multi-type parameters
A generic class may have more than one type of parameter. As shown below:
public interfacePair<k,v>{Public K GetKey ();Public V GetValue ();}public class Orderedpair<k,v> ImplementsPair<k,v>{Private K key;private V value;Public Orderedpair (K key,v value) {This. Key= key; This. value= value; } @OverridePublic K GetKey () {Return key; } @OverridePublic V GetValue () {return value; }}Instantiation ofPair<String,Integer> P1=New Orderedpair<String,Integer> ("Even",8);Pair<String,String> P2=New Orderedpair<String,String> ("Hello","World");The new syntax for Java SE7 begins to appear Orderedpair<String,Integer> P1=New Orderedpair<> ("Even",8); Orderedpair<string, String> p2 = new orderedpair <> ( "hello", "world"); Parameterized type Orderedpair<string, Box <integer>> p = new orderedpair<> ( "primes", new box<integer> (
Java Programming thought Learning (11) Generics