简体   繁体   中英

Unchecked cast warnings with parameterized types

I am reading about restrictions on generics from the Java Tutorials, specifically casting with parameterized types . I understand the examples that are presented. However, I am unsure about the following example:

List<? extends Number> l1 = new ArrayList<Integer>();
// unchecked cast warning
ArrayList<Number> l2 = (ArrayList<Number>) l1;
// no unchecked cast warning
ArrayList<? extends Number> l3 = (ArrayList<? extends Number>) l1;

I understand why there is a warning in the first case. Why is there no warning in the second case? Is it because the only operations that I can perform on l3 are safe (eg, I cannot add (say) a Float to the list)?


UPDATE: Below is an excerpt from section 5.5.2 of the JLS that addresses similar questions.

A cast from a type S to a parameterized type (§4.5) T is unchecked unless at least one of the following is true:

  • S <: T
  • All of the type arguments (§4.5.1) of T are unbounded wildcards
  • T <: S and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T .

There's no warning because the cast doesn't change the generic part of the type. List<? extends Number> List<? extends Number> and ArrayList<? extends Number> ArrayList<? extends Number> have the same type argument. Instead, the cast is a checked cast from List to ArrayList .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM