Sub topic 13
Writing with ? super
In the last chapter, ? extends let you read from a list safely. Now you will learn the opposite tool. ? super lets you write into a list safely.
What `? super` Means
A lower-bounded wildcard is a wildcard that sets the lowest type a list can hold. You write it as ? super Type.
List<? super Integer> means "a list of Integer, or of any type above Integer." A type above Integer is a supertype, a class that Integer inherits from. Integer's supertypes include Number and Object.
So these three lists all fit:
| Declared list | Fits `List<? super Integer>`? | Why |
|---|---|---|
| `List<Integer>` | Yes | Integer is the bound itself |
| `List<Number>` | Yes | Number is a supertype of Integer |
| `List<Object>` | Yes | Object is a supertype of everything |
| `List<Double>` | No | Double is not a supertype of Integer |
Why Writing Is Safe
Whichever list you get, it can hold an Integer. A List<Number> can hold an Integer, and so can a List<Object>. That means the compiler lets you call add with an Integer.
Here is a complete program:
import java.util.ArrayList;
import java.util.List;
public class SuperDemo {
static void fillWithCounts(List<? super Integer> dst, int n) {
for (int i = 1; i <= n; i++) {
dst.add(i); // safe: every allowed list can hold an Integer
}
}
public static void main(String[] args) {
List<Integer> ints = new ArrayList<>();
List<Number> numbers = new ArrayList<>();
List<Object> objects = new ArrayList<>();
fillWithCounts(ints, 3);
fillWithCounts(numbers, 3);
fillWithCounts(objects, 3);
System.out.println(ints); // [1, 2, 3]
System.out.println(numbers); // [1, 2, 3]
System.out.println(objects); // [1, 2, 3]
}
}The single method works for all three lists. If the parameter were List<Integer>, the calls with numbers and objects would not compile.
The Cost: Reading Gives You Object
Reading is where ? super falls short. The compiler does not know whether the list is a List<Integer>, a List<Number>, or a List<Object>. The only type every one of those lists is sure to hold is Object.
import java.util.ArrayList;
import java.util.List;
public class SuperReadDemo {
public static void main(String[] args) {
List<? super Integer> dst = new ArrayList<Number>();
dst.add(10);
Object item = dst.get(0); // allowed
// Integer x = dst.get(0); // compile error: result is only Object
// dst.add(2.5); // compile error: 2.5 is a Double, not an Integer
System.out.println(item); // 10
}
}You can still read, but you only get Object back. You also cannot add a Double, even into a list that really holds Numbers. The compiler only promises that Integers are safe.
Common Mistakes
- Writing
List<? super Number>and expecting to pass aList<Integer>. Integer is below Number, so this fails. - Expecting
getto return the bound type. It returnsObject. - Adding a sibling type such as Double to a
List<? super Integer>.
Exercise
You have a method static void addZero(List<? super Integer> list) { list.add(0); }. Which of these calls compile: addZero(new ArrayList<Integer>()), addZero(new ArrayList<Number>()), addZero(new ArrayList<Object>()), addZero(new ArrayList<Double>())?
Show answer
The first three compile, because Integer, Number, and Object are each Integer or a supertype of Integer. The Double call does not compile, because Double is not a supertype of Integer.
Exercise
Inside a method with the parameter List<? super Integer> list, what type does list.get(0) return, and why?
Show answer
It returns Object. The list could be a List<Integer>, a List<Number>, or a List<Object>, and Object is the only type that is guaranteed for all of them.