Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Java Generic Wildcards

Tech Jun 12 1

Java's wildcard character ? serves as a special type parameter representing an unknown type. Wildcards enhance code flexibility when working with generic types, allowing generic containers to reference various generic objects. There are three primary wildcard usage scenarios: unbounded wildcards, upper-bounded wildcards, and lower-bounded wildcards.

1. Unbounded Wildcard (?)

The unbounded wildcard ? denotes any type, primarily used when reading or manipulating collections without concern for the specific element type.

public void displayItems(List<?> items) {
    for (Object element : items) {
        System.out.println(element);
    }
}

Here, List<?> represents a list of unknown type elements, enabling the displayItems method to accept any List instance.

2. Bounded Wildcards

Bounded wildcards restrict the unknown type's range, including upper bounds (? extends Type) and lower bounds (? super Type).

Upper-Bounded Wildcard (? extends Type)

The upper-bounded wildcard ? extends Type specifies that any type extending Type is acceptable.

public void showNumbers(List<? extends Number> numbers) {
    for (Number num : numbers) {
        System.out.println(num);
    }
}

List<? extends Number> accepts List<Integer>, List<Double>, or any other Number subclass list.

Lower-Bounded Wildcard (? super Type)

The lower-bounded wildcard ? super Type permits lists to reference Type or any of its supertypes.

public void insertValue(List<? super Integer> container) {
    container.add(42); // Accepts Integer or its supertypes
}

List<? super Integer> could be List<Integer>, List<Number>, or List<Object>.

Wildcard Usage Guidelines

  • Use ? extends T when reading T elements from a collection without writing (producer).
  • Use ? super T when writting T elements to a collection without reading (consumer).
  • Use the unbounded wildcard ? when neither reading nor writing is required.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.