Overview
When I was learning about generics in Java, it was very easy to understand at first, but when I went to the wildcard, I fainted because the idea of learning Java was the guiding ideology of this piece of content, Compared to. NET, but in Java the wildcard character,. NET does not have such a concept, so take the time to learn a bit, the following examples to explain to you the wildcard character
Instance Code
Package Com.tgb.mydemo;import Java.util.arraylist;import Java.util.list;public class Test {public static void main ( String[] args) {integer[] integers = {1,2,3,4,5}; arraylist<integer> integerlist = new arraylist<integer> (); for (Integer integer:integers) {IntegerList.add (integer);} SYSTEM.OUT.PRINTLN ("Total of the elements in Integerlist:" + sum (integerlist));D ouble[] doubles = {1.1,2.2,3.3,4.4,5.5}; arraylist<double> doublelist = new arraylist<double> (); for (Double element:doubles) {Doublelist.add ( Element);} SYSTEM.OUT.PRINTLN ("Total of the elements in Integerlist:" + sum (doublelist)); number[] numbers = {1.1,2.2,3,4,5.5}; arraylist<number> numberlist = new arraylist<number> (); for (number Element:numbers) {Numberlist.add ( Element);} SYSTEM.OUT.PRINTLN ("Total of the elements in Integerlist:" + sum (numberlist));} public static double sum (arraylist<number> list) {Double total = 0;for (number element:list) {Total + = Element.doubl Evalue ();} return total;}}
Note: number is the parent class of double and Integer
Put the above code in the Java IDE, there will be a syntax error, why? Because the argument type of the SUM function is arraylist<number>, when we use the SUM function, we pass the arraylist<integer> or arraylist<double> type to it. Although the number type is a Double and an Integer type of parent, the compiler does not put arraylist<number> and arraylist<integer> (arraylist<double >) as the same type of view, so, at this time, will be in code editing time, error, if we sum the parameter type into ARRAYLIST< Extends Number>, and solved the problem, and "? This is the wildcard character, which is why wildcards appear.
the difference between a wildcard and a generic constraint
The generic constraint here is defined as a generic constraint when defining a generic class, from which we know that a generic constraint is used when defining a generic class, a constraint on generics when a generic class is defined, and a wildcard is a constraint on a type when using a generic class.
types of constraints on wildcard characters
There are 3 restrictions on wildcard characters.
Unconstrained: <?>, that is, the parent class of the type must be of type object.
Upper bound: <? Extends type 1>, that is, the type must be a subclass of type 1 or type 1.
downline constraint: <? Super type 1>, that is, the type must be a parent class of type 1 or type 1.
Summary
Wildcards make it possible for us to use generic classes to give specific types of situations, turning specific types into parameterized cases, making the end user's scope smaller.
Wildcard characters in Java generics