Raw types, unlimited plug and limited wildcard

I have a quick question, as shown below: Here are simple examples on all of these issues:

List a = new ArrayList(); List <?> b; List <? extends Object> c; 

According to Java SCJP from khalid mughal (a very good book!):

 a = b; // ok. Widening conversion. b = a; // ok too. No unchecked warning. b = c; // ok c = b; // ok c=a; // ok but now will issue a unchecked warning. // clause 1 

I understand that any raw types (example a) when assigning to any restricted links to wilcard with an unanswered warning are problems (since the contents in this raw type can be any).

My questions, since c is the upper upper bound (? Extends objects), should c not be assigned without this warning?

+2
java generics bounded-wildcard
source share
1 answer

If I understand your question correctly (and I really don't think so), there seem to be two cases where interacting with raw types can lead to an uncontrolled warning, according to this page :

  • A call to a method or constructor of a raw type generates an unchecked warning if erasing changes any of the types of any of the arguments to the method or constructor.
  • Assigning an unprocessed type to a field generates an unchecked warning (ยง5.1.9) if erasing changes the type of the field.

So the answer to your question basically seems " erasure can lead to uncontrolled warnings when there are raw types". As far as I can tell, this is most likely to happen when nested types are used - I don't see anywhere in the deletion definition that is likely to cause a type change, but maybe someone else can guess if this is not the source of this.

+1
source share

All Articles