A wildcard type uses ? as a Java generic argument to express an unknown type. The bounded form ? extends CharSequence accepts a List of one particular CharSequence subtype, such as String, while preventing ordinary additions through that reference. section 5.3.2
Accepting a more specific list safely
A method declared with List<CharSequence> cannot accept a List<String>: callers could add another CharSequence implementation. Declaring it as List<? extends CharSequence> accepts the String list while preserving the guarantee that the method will not add incompatible elements. section 5.3.2
Prefer these practices when a generic collection is consumed without modification:
- Use a bounded wildcard in the method signature instead of upcasting and applying an unchecked cast. section 5.3.2
- If a signature cannot change, pass an unmodifiable wrapper; its List<? extends T> parameter can adapt a more concrete list to a more abstract result. section 5.3.2
- Treat unchecked generic casts as a last resort: type erasure can defer the resulting ClassCastException until a later use. section 5.5
