Wildcards in Java Generics allow methods and variables to work with different generic types without specifying an exact type. They are represented by the ? symbol and provide flexibility while maintaining type safety.
- Allow methods to accept collections with different generic type arguments.
- Help control which types can be read from or added to a collection.
- Commonly used with generic methods and collections.
Types of Wildcards
Java provides three types of wildcards based on their bounds:
1. Upper Bounded Wildcards
An upper bounded wildcard restricts the unknown type to a specified class or its subclasses. It is declared using ? extends followed by the upper bound.
Syntax:
List<? extends Number> list;
Here, ? extends Number accepts Number itself and its subclasses, such as Integer, Double, and Float.
Example: Demonstrating upper bounded wildcards using different numeric types.
import java.util.Arrays;
import java.util.List;
class Geeks{
public static void main(String[] args){
List<Integer> list1 = Arrays.asList(4, 5, 6, 7);
List<Double> list2 = Arrays.asList(4.1, 5.1, 6.1);
System.out.println("Integer Sum: " + sum(list1));
System.out.println("Double Sum: " + sum(list2));
}
static double sum(List<? extends Number> list){
double total = 0;
for (Number num : list){
total += num.doubleValue();
}
return total;
}
}
Output
Integer Sum: 22.0 Double Sum: 15.299999999999999
Explanation:
- List<? extends Number> accepts lists of Number or its subclasses.
- The sum() method can process both Integer and Double lists.
- Elements can be read as Number objects.
- Adding an Integer, Double, or other Number value is not normally allowed because the exact list type is unknown.
2. Lower Bounded Wildcards
A lower bounded wildcard allows a generic type to accept a specified class or its superclasses. It is declared using ? super followed by the lower bound.
Syntax:
List<? super Integer> list;
Here, ? super Integer accepts lists of Integer, Number, or Object.
Example: Demonstrating how to add elements using lower bounded wildcards.
import java.util.ArrayList;
import java.util.List;
class Geeks{
public static void main(String[] args){
List<Integer> list1 = new ArrayList<>();
List<Number> list2 = new ArrayList<>();
addNumbers(list1);
addNumbers(list2);
System.out.println("Integer List: " + list1);
System.out.println("Number List: " + list2);
}
static void addNumbers(List<? super Integer> list){
list.add(10);
list.add(20);
}
}
Output
Integer List: [10, 20] Number List: [10, 20]
Explanation:
- List<? super Integer> accepts lists of Integer and its superclasses.
- The addNumbers() method adds Integer values to both lists.
- Reading elements from this list generally returns Object because the exact generic type is unknown.
3. Unbounded Wildcards
An unbounded wildcard is represented by <?>. It allows a method to accept a generic collection of any type without specifying its actual type argument.
Syntax:
List<?> list;
It is useful when a method needs to perform operations that do not depend on the specific element type, such as displaying collection elements.
Example: Demonstrating unbounded wildcards with different types of lists.
import java.util.Arrays;
import java.util.List;
class Geeks{
public static void main(String[] args){
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Double> list2 = Arrays.asList(1.1, 2.2, 3.3);
List<String> list3 = Arrays.asList("Java", "Python");
printList(list1);
printList(list2);
printList(list3);
}
static void printList(List<?> list){
for (Object element : list) {
System.out.print(element + " ");
}
System.out.println();
}
}
Output
1 2 3 1.1 2.2 3.3 Java Python
Explanation:
- List<?> accepts lists containing any reference type.
- Elements can be retrieved as Object.
- Non-null elements cannot normally be added because the actual element type is unknown.
- The method can display elements without knowing their specific types.
Difference Between Upper, Lower, and Unbounded Wildcards
Feature | Upper Bounded | Lower Bounded | Unbounded |
|---|---|---|---|
Syntax | ? extends T | ? super T | ? |
Bound | Upper bound | Lower bound | No explicit bound |
Accepts | T and subclasses | T and superclasses | Any reference type |
Reading | Read as T | Generally read as Object | Read as Object |
Adding | Generally not allowed | T and its subclasses can be added | Non-null values cannot be added |
Common Use | Reading data | Adding data | Reading or processing unknown types |