When you read from a `List<? super Integer>`, what type do you get back, and why?
answer
- ? super = unknown supertype
- read returns Object
- write a T is safe
- Consumer Super half of PECS
- cast needed to get T back
basics
~10 sYou get back Object. The list could be a List<Object>, so the only type the compiler can guarantee for every element is Object. You can safely add Integers, but reads come back as Object.
solid answer
~40 s`List<? super Integer>` means "a list of some unknown supertype of Integer" — it could be a `List<Integer>`, `List<Number>`, or `List<Object>`. Writing is safe: an `Integer` fits into any of those. But reading is the problem. The compiler cannot tell how far up the hierarchy the real element type goes, so the only guaranteed upper bound is `Object`. Therefore `get()` returns `Object`, and you would need a cast (with the usual risk) to treat it as anything more specific. This is the "Consumer Super" half of PECS: a `? super T` collection is a consumer — great for putting T's in, weak for getting them out. If you mainly need to read typed values, use `? extends T` instead.
code
java · 6 linesList<? super Integer> sink = new ArrayList<Number>();
sink.add(1); // OK: Integer fits any supertype of Integer
sink.add(2); // OK
Object o = sink.get(0); // read type is Object
// Integer i = sink.get(0); // compile error
// sink.add(3.14); // compile error: Double is not <= Integergo deeper
Knows reads from a ? super list come back as Object and that you can add the lower-bound type.
Explains the lower-bound semantics, why Object is the only safe read type, and the consumer role in PECS.
Chooses super vs extends deliberately in API design and explains the asymmetry from assignability rules.
Guides variance conventions, weighs the caller-side cost of Object reads, and reviews library signatures for correct producer/consumer roles.
## The meaning of `? super Integer` `? super Integer` is a **lower-bounded wildcard**. The `?` is one specific but unknown type, and `super Integer` says that type is Integer itself or some **supertype** of Integer. Integer's supertypes include `Number`, `Comparable<Integer>`-related types, and ultimately `Object`. So a `List<? super Integer>` variable might really refer to a `List<Integer>`, a `List<Number>`, or a `List<Object>`. ## Why writing is safe An `Integer` is-a Integer, is-a Number, is-a Object. Whatever the real element type is (Integer or higher), an `Integer` is assignable to it. So `list.add(42)` is always type-safe. You may also add subtypes of Integer (there are none in practice, but the rule holds for any `? super T`). ## Why reading degrades to Object When you read, the compiler must give the result a type that is valid no matter which supertype the list really holds. The list could be a `List<Object>`, whose elements are merely `Object`. There is no tighter guarantee that holds in every case, so `get()` is typed as `Object`: ```java List<? super Integer> list = new ArrayList<Number>(); Object o = list.get(0); // OK Integer i = list.get(0); // compile error: Object is not assignable to Integer ``` To recover a more specific type you must cast, which reintroduces the runtime risk generics were meant to remove. ## The PECS link This is the **Consumer Super** half. A `? super T` parameter is a *consumer*: the method pushes T values into it. You declare `Collection<? super T>` for something like `addAll`-style sinks. If instead you mostly need to read typed values out, that is a *producer* and you want `? extends T`. ## Comparison table | Wildcard | Add a T? | Read returns | Role | |---|---|---|---| | `? extends T` | No (null only) | `T` | Producer | | `? super T` | Yes | `Object` | Consumer | | `T` (exact) | Yes | `T` | Both | ## Terms defined - **Upper-bounded wildcard:** `? extends T`, unknown subtype of T. - **Lower-bounded wildcard:** `? super T`, unknown supertype of T. - **Assignable:** value v is assignable to type X if v's type is X or a subtype of X. The whole behavior follows mechanically from "what can the compiler prove for every possible real type the wildcard might stand for."
- Can you add a `Number` to a `List<? super Integer>`?No. The list could be a `List<Integer>`, and a Number is not necessarily an Integer. You can only add Integer or its subtypes — the lower bound.
- How would you get a typed value out of a `? super T` list safely?You generally cannot do it type-safely; you read it as Object and cast, accepting the risk. If you need typed reads, the parameter should have been `? extends T`.
saying these in an interview costs you the question
- Saying reads return T (they return Object)
- Thinking you cannot write to a `? super T` list (writing T is the safe operation)
- Confusing super (lower bound) with extends (upper bound)
- Assuming a cast on read is free of runtime risk