Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A lower bounded wildcard in Java restricts an unknown generic type to be a specific class or any of its superclasses. Denoted by the <? super T> syntax, it establishes a lower limit on the type hierarchy, meaning the parameterized type must be T or an ancestor of T (up to java.lang.Object).

Syntax

The wildcard is declared using the ? character, followed by the super keyword, and then the lower bound type.
List<? super Number> lowerBoundedList;

Assignment Compatibility

When a reference is declared with a lower bounded wildcard, it can point to a generic object parameterized with the exact type or any of its supertypes. For example, a List<? super Number> can accept:
  • List<Number>
  • List<Object>
It will reject List<Integer> or List<String>, resulting in a compile-time error, because neither Integer nor String are superclasses of Number.

Read and Write Mechanics

The primary technical implication of a lower bounded wildcard dictates how the compiler handles reading from and writing to the generic structure.

Writing (Insertion)

You can safely add instances of the lower bound type T, or any of its subclasses, into the structure. The compiler allows this because a collection designed to hold T or any superclass of T is inherently capable of holding a T (and by extension, any valid subclass of T).

Reading (Retrieval)

Reading from a lower bounded wildcard is highly restricted. Because the compiler only knows that the collection holds some supertype of T, it cannot guarantee the specific type of the elements being retrieved. The only safe assumption the compiler can make is that the elements are instances of java.lang.Object.

Code Visualization

// The reference can point to a List of Number or Object
List<? super Number> list = new ArrayList<Object>();

// 1. WRITING: Allowed for Number and its subclasses
list.add(Integer.valueOf(10));  // Integer is a subclass of Number
list.add(Double.valueOf(20.5)); // Double is a subclass of Number

// WRITING: Rejected for superclasses or unrelated types
// list.add(new Object()); // Compile-time error (Object is a superclass, not a subclass)
// list.add("String");     // Compile-time error (String is unrelated to Number)

// 2. READING: Elements lose their specific type identity
Object obj = list.get(0); // Allowed: Guaranteed to be at least an Object

// READING: Rejected without explicit downcasting
// Number n = list.get(0);  // Compile-time error
// Integer i = list.get(0); // Compile-time error

Type System Role

In Java’s generic type system, lower bounded wildcards implement contravariance. They invert the standard subtyping relationship, allowing a generic type to accept a wider, more generic type argument. This mechanic enforces type safety for operations that mutate (consume) data, ensuring that heap pollution does not occur when passing generic collections across different API boundaries.
Master Java with Deep Grasping Methodology!Learn More