Java Generics Upper Bounded Wildcards Example

Introduction

Upper bounded wildcards in Java Generics restrict the unknown type to be a specific type or a subtype of that type. This is useful when you want to read items from a structure and ensure that the elements are of a certain type or its subclass. Upper bounded wildcards are declared using the extends keyword.

Table of Contents

  1. What are Upper Bounded Wildcards?
  2. Syntax of Upper Bounded Wildcards
  3. Example: Upper Bounded Wildcards
  4. Explanation of the Example
  5. Use Cases for Upper Bounded Wildcards
  6. Conclusion

1. What are Upper Bounded Wildcards?

Upper bounded wildcards allow you to specify that a type parameter must be a specific type or a subclass of that type. This ensures type safety and allows you to use methods and properties defined in the upper bound type.

2. Syntax of Upper Bounded Wildcards

The syntax for upper bounded wildcards is to use the extends keyword followed by the upper bound type within angle brackets (<>).

Syntax:

List<? extends Type> list = new ArrayList<>();

3. Example: Upper Bounded Wildcards

Example:

import java.util.List;
import java.util.ArrayList;

public class UpperBoundedWildcardExample {
    // Method to calculate the sum of a list of numbers
    public static double sumOfList(List<? extends Number> list) {
        double sum = 0.0;
        for (Number number : list) {
            sum += number.doubleValue();
        }
        return sum;
    }

    public static void main(String[] args) {
        List<Integer> intList = new ArrayList<>();
        intList.add(1);
        intList.add(2);
        intList.add(3);

        List<Double> doubleList = new ArrayList<>();
        doubleList.add(1.1);
        doubleList.add(2.2);
        doubleList.add(3.3);

        System.out.println("Sum of intList: " + sumOfList(intList));
        System.out.println("Sum of doubleList: " + sumOfList(doubleList));
    }
}

Output:

Sum of intList: 6.0
Sum of doubleList: 6.6

4. Explanation of the Example

  • Method Definition: The sumOfList method accepts a list of any type that extends Number. This means the list can contain Integer, Double, Float, etc.
  • Iterating and Summing: The method iterates through the list, treating each element as a Number and summing their double values.
  • Usage: In the main method, two lists (intList and doubleList) are created and populated with integers and doubles, respectively. The sumOfList method is called with each list, demonstrating that it can handle both Integer and Double types.

5. Use Cases for Upper Bounded Wildcards

  • Reading from a Collection: When you want to read elements from a collection and perform operations on them, using an upper bounded wildcard ensures that the elements are of a specific type or its subtype.
  • Generic Algorithms: Upper bounded wildcards are useful in generic algorithms that need to operate on elements of a certain type or its subtypes.
  • Type Safety: They help ensure type safety by restricting the types that can be used as arguments, preventing potential runtime errors.

6. Conclusion

Upper bounded wildcards in Java Generics provide a powerful way to write flexible and type-safe code. By specifying that a type parameter must be a specific type or a subtype of that type, you can ensure that your methods can safely operate on the elements of a collection. Understanding and using upper bounded wildcards can enhance the robustness and reusability of your Java programs.

Happy coding!

Comments