Hope to understand: one. Benefits of generics two. Expression Three. The role of <r extends t> four. <? Super T>,<? Extends t> acts as a parameter type five. Wildcard nested <? Extends|super? Extends|super t>. JDK1.5 introduced generics, benefits: 1. At compile time, type Check 2. Avoid type conversions, for example,
New ArrayList (); List.add ("str1") System.out.prinntln ((String) List.get (0)). Length ());
the writing of JDK1.4Defects: 1. There is no type guarantee if List.add (new Object ()); Then (String) list.get (0); Will be ClassCastException2. It is much more convenient to cast the use of generics without using the elements in list.
New Arraylist<string>(); List.add ("str1"); System.out.println (List.get (0). Length ());
JDK1.5 notationFor JDK1.7 or more, further simplification:
New Arraylist<> ();
JDK1.7 notation
The type is automatically added later;
Two. Several representations: 1. Generic static functions
Public class C { publicstatic <T> t foo (t v) { returnnull ; }} // Usage: String ret = C.foo ("str");
2. Generic class
Public class C<t> { public T foo (t v) { returnnull; }} // Usage: New c<string>= Instance.foo ("str");
3. Generic member functions
Public class C { public <T> T foo (t v) { returnnull;} } // Usage: New = Instance.foo ("str");
4. Generic member functions for generic classes
Public class C<r> { public <T> t foo (t v1, R v2) { returnnull; }} // Usage: New c<string>= Instance.foo (1, "str");
5. Generic static functions for generic classes
Public class C<r> { publicstatic <T> t foo (t v1) { return Null;} } // (the generic of R does not make sense) Integer ret = C.foo (1);
6. Public class C<t> {public static T foo (T v) {return null;}}
cannot compileThree., <r extends t> function as the type of method, extends provides the upper limit of R, such as <r extends Number>,r can be number,integer,double and other inherited Numberl class, Play a limiting role. No <r super T>, such as <r Super Number>,r can be number, Serializable, because subclasses can be placed on the parent class, equivalent to number, so
It doesn't make senseof such as
Public class C { publicstaticextends number> T foo (R v) { return v; = C.foo (1);
As a type of class, it is similar to
Public class extends Number> { public R foo (r v) { return v; }} CNew c<integer>= Instance.bar (1);
So, what are the benefits of using <r extends number>: 1. V can know that it is number or subclass, can use the number method earlier, V.intvalue (); 2. Now the scope of materialization, such as the c<string> compilation error, is also not supported for class C<r Super number>. Four. <? Super T>,<? Extends t> as a parameter type first of all, generic delete <?> represents no type, if you are a parameter type, you can only receive null values, and if you are a return type, you can only return object; Generally this class can only be used with functions that have no generics, or that Do not care about its type such as
public class C<t> { public T foo (t v) { return V; public String Bar (int i) { return "" + I; }} C <?> instance = new c<?> () ; Object ret = Instance.foo (null ); // This usage has no practical effect String Sret = Instance.bar (1); // main call no type part
Since c<?> does not have to consider the type of T, then materialization is also possible, such as c<?> instance = new C<string> ();
In this way, the creator can set the type according to his own needs, and the user does not need the relationship type
If you need to use a data type, you can use C<? Super number> or c<? Extends Number>c<? Super Number> is suitable for incoming type, returns no type, the container can be backwards compatible as
Super New c<object>= Instance.foo (new// accepts number parameter; but returns the value Object, which is typically used to modify the data.
c<? The extends number> is suitable for return types, and cannot pass in parameters, and the container can be up compatible as
extends New c<integer>= Instance.foo (null// does not receive parameters, but returns number, typically used to fetch data.)
Five. Wildcard nested <? Extends|super? Extends|super t> This part actually
It doesn't make sense ., can be regarded as <? Super t> and <? Extends T> only for the completeness of the language the following examples illustrate:
Public classC<t> { Publicc<?Supert> foo (c<?SuperT>v) {returnv; } Publicc<?extendsT> bar (c<?extendsT>v) {returnv; }} C<?Supernumber> s =NULL; C<?extendsNumber> e =NULL; C<?SuperNumber> sinstance =NewC<object>(); C<?Supernumber> Ret1 = Sinstance.foo (NULL); C<?> Ret2 =Sinstance.bar (e); C<?extendsNumber> einstance =NewC<integer>(); C<?> Ret3 =Einstance.foo (s); C<?extendsnumber> Ret4 = Einstance.bar (NULL);View CodeSummarized as follows: <? Super? Super t> parameter type: null, return value <? Super T><? Extends? Super t> parameter type: <? Extends T>, return value <?><? Super? Extends t> parameter type <? Super T>, return value <?><? Extends? Extends t> parameter type is NULL, return value <? Extends t>
Understanding of Java Generics