Java generics, Unbound wildcards <?> vs <Object> -
i've read few topics cover questions generics, such relationship raw types. i'd additional explanation on line found in java se tutorial on unbound generics .
according sentence :
the goal of printlist print list of type, fails achieve goal — prints list of object instances; cannot print list<integer>, list<string>, list<double>, , on, because not subtypes of list<object>.
if understand sentence; difference between list<?> , list<object>, can use type argument list<string> or list<integer> implementing former. while if implement later, can use type argument list<object>. if list<?> upper bound object namely list<? object>.
but following sentence confuses me, in sense according understood, list<object> should contain instances of class object , not else.
it's important note
list<object>,list<?>not same. can insert object, or subtype of object,list<object>. can insertnulllist<?>.
there 2 separate issues here. list<object> can in fact take object say. list<number> can take at least number objects, or of course subclasses, integer.
however method this:
public void print(list<number> list); will only take list exactly list<number>. not take list declared list<integer>.
so difference list<?> take list whatever declaration, list<object> only take declared list<object>, nothing else.
the last quote states, list<?> list literally don't know type items are. because of that, can not add other null.
Comments
Post a Comment