~/ 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/6b691496-30e0-414d-84d2-d9356b283af9/flashcard utf-8 LF