Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Generic method inference and boxing

Java can infer a generic method’s type arguments from the call arguments and, where applicable, the expected result type. Boxing lets an `int` argument take part in such a call as an `Integer`, not as a primitive type argument.

Version: Java SE 21

Syntax

static <T> T echo(T value) { return value; }
static <T extends Comparable<? super T>> T min(T a, T b) { return a.compareTo(b) <= 0 ? a : b; }
static <T> void swap(T[] values, int i, int j) { T temp = values[i]; values[i] = values[j]; values[j] = temp; }

String echoString = echo("Java");
Integer minInteger = min(7, 3);
String explicit = Main.<String>echo("Java");

Generic method inference and boxing are separate steps to keep track of in Java SE 21. In static <T> T echo(T value), <T> declares the method’s type parameter before the return type T. The compiler infers String for echo("Java"). A target type can also guide inference: in String text = echo(null);, the assignment supplies type information that null alone does not. You can specify the type explicitly with Main.<String>echo("Java").

Neither echo nor swap declares an explicit bound: T has the implicit upper bound Object. swap exchanges two elements in the original array without comparing them. It works with String[] but not with int[]: Java does not automatically box an entire int[] into an Integer[].

min uses <T extends Comparable<? super T>>: values of type T must support comparison with other T values through compareTo, which can accept T or a supertype of T. The method returns the smaller value, or the first on a tie. In min(7, 3), the compiler infers Integer, and the int arguments are boxed. int itself cannot be a type argument. The assignment int minimum = minInteger; unboxes the result; unboxing null throws NullPointerException.

Overload resolution first looks for applicable methods without boxing or unboxing. If none apply, it allows those conversions; variable-arity invocation is considered only in the last phase. Adding a min(int, int) overload to the example would therefore change which method min(7, 3) calls.

Examples

public class Main {
    static <T> T echo(T value) {
        return value;
    }

    static <T extends Comparable<? super T>> T min(T a, T b) {
        return a.compareTo(b) <= 0 ? a : b;
    }

    static <T> void swap(T[] values, int i, int j) {
        T temp = values[i];
        values[i] = values[j];
        values[j] = temp;
    }

    public static void main(String[] args) {
        String echoString = echo("Java");
        Integer minInteger = min(7, 3);
        int minimum = minInteger;

        String[] words = {"left", "right"};
        swap(words, 0, 1);

        System.out.println(echoString);
        System.out.println(minimum);
        System.out.println(words[0] + ", " + words[1]);
    }
}

Result

Java
3
right, left

The first line shows the result of `echo`, the second shows the unboxed minimum, and the third shows the order of the elements in the same array after `swap`.

Related lessons

  • Java · Lesson 15 · Advanced · Version: 2

Sources