Java language

Upper-bounded wildcard

Allows lists whose unknown element type is bounded above by Number; reading as Number is safe.

Formal Syntax & Structure

java syntaxjava
List<? extends Number>

Working Example

Example Snippetjava
import java.util.*;public class Main {    static <T extends Number> double twice(T value) { return 2 * value.doubleValue(); }    static double sum(List<? extends Number> values) {        double result = 0;        for (Number n : values) result += n.doubleValue();        return result;    }    public static void main(String[] args) {        System.out.println(twice(3));        System.out.println(twice(1.25));        System.out.println(sum(List.of(2, 3)));    }}
Java Reference