J.I · Java 25 practice question

What does this Java 25 program print when adding an int and a long?

void main() {
    int base = 5;
    long offset = 7L;
    var total = base + offset;
    System.out.println(((Object) total).getClass().getSimpleName());
}
Choose one answer
Check answer and explanation

Correct answer: Long

Explanation

With no double or float operand present, having a long operand makes the promoted type long, widening the int to long and boxing to Long.

Common trap

The int operand is widened to long rather than narrowing the long to int. Integer arithmetic never promotes automatically to floating-point types like float.

Source: JLS SE 25 §5.6. Numeric Contexts
Checked against the cited text on 2026-11-25.

Report an error in this question