java - Difference between wildcard type vs bounded type -
this question has answer here:
- java generics (wildcards) 6 answers
- what difference between bounded wildcard , type parameters? 2 answers
public static void main(string args[]) { list<double> doublelist = new arraylist<>(); doublelist.add(new double(101.215d)); doublelist.add(new double(102.215d)); doublelist.add(new double(103.215d)); printintvalue1(doublelist); system.out.println("*******"); printintvalue2(doublelist); } //bounded parameter public static <t extends number> void printintvalue1(list<t> list){ for(t num : list){ system.out.println(num.intvalue()); } } //wildcard parametr public static void printintvalue2(list<? extends number> list){ for(number num : list){ system.out.println(num.intvalue()); } }
as in above 2 methods, both gives same results. can tell me if work done bounded type why concept of wildcard there? wildcard other work bounded types don't?
i think perspective of example, there little benefit gained using wildcards. believe, however, wildcards introduced support assignments (or related constructs) of generic types using related type parameters.
consider:
list<number> numberlist = new arraylist<double>(); //won't compile
contrast to:
list<? extends number> anynumberlist = null; anynumberlist = new arraylist<double>(); anynumberlist = new arraylist<integer>();
the second snippet valid code.
i agree that, when seen method parameter type perspective, wildcard type doesn't present considerably particular advantages.
Comments
Post a Comment