~/ learn/ java-from-zero/ cards/ Gotcha roundup II — types & objects
1 of 4

Type a call showing widening wins over autoboxing in overload resolution

Type a call showing widening wins over autoboxing in overload resolution

Answer

void print(long x) { System.out.println("long"); } void print(Integer x) { System.out.println("Integer"); } // print(5) prints "long" — widening before boxing

Java resolves to the widening overload (long) rather than the boxing overload (Integer) because widening is preferred. The resolution order is: exact match, then widen, then box, then varargs.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/e17f6566-5fd0-4dc0-a6b0-8b23459362fb/flashcard utf-8 LF