0
0
JavaConceptBeginner · 4 min read

What is Wildcard in Java Generics: Simple Explanation and Examples

In Java generics, a wildcard is a special symbol represented by ? that stands for an unknown type. It allows you to write flexible and reusable code by accepting different types without specifying exact ones.
⚙️

How It Works

Imagine you have a box that can hold items, but you don't know exactly what kind of items it holds. The wildcard ? in Java generics works like a label on that box saying "I can hold any type of item." This lets you write code that can work with many types without fixing one.

For example, if you have a list of fruits, a list of vegetables, or a list of toys, you can use a wildcard to say "I accept a list of something," without caring what exactly is inside. This makes your code more flexible and easier to reuse.

There are also special forms of wildcards like ? extends Type which means "any type that is a subtype of Type," and ? super Type which means "any type that is a supertype of Type." These help control what you can read or write safely.

💻

Example

This example shows how to use a wildcard to accept a list of any type and print its elements.

java
import java.util.List;

public class WildcardExample {
    public static void printList(List<?> list) {
        for (Object elem : list) {
            System.out.println(elem);
        }
    }

    public static void main(String[] args) {
        List<Integer> intList = List.of(1, 2, 3);
        List<String> strList = List.of("apple", "banana", "cherry");

        printList(intList);
        printList(strList);
    }
}
Output
1 2 3 apple banana cherry
🎯

When to Use

Use wildcards when you want to write methods or classes that can work with different types of generic objects without fixing the exact type. This is especially useful when you only need to read from a collection or when you want to accept a range of types related by inheritance.

For example, if you have a method that processes a list of animals, but you want it to accept lists of dogs, cats, or any other animal subtype, you can use ? extends Animal. If you want to add elements safely to a collection, you might use ? super Type.

Wildcards help make your code more flexible, reusable, and safe by controlling what types are allowed and how they can be used.

Key Points

  • The wildcard ? represents an unknown type in generics.
  • ? extends Type means any subtype of Type, useful for reading.
  • ? super Type means any supertype of Type, useful for writing.
  • Wildcards increase flexibility and reusability of generic code.
  • They help avoid type errors by restricting what operations are allowed.

Key Takeaways

A wildcard ? in Java generics stands for an unknown type to allow flexible code.
Use ? extends Type to accept any subtype when you only read data.
Use ? super Type to accept any supertype when you only write data.
Wildcards help make generic methods and classes reusable with different types.
They prevent type errors by controlling how generic types can be used.