A method declares two local variables:byte a = 10;
byte b = 20;Which statement, added after them, compiles?
Choose one.
Binary numeric promotion turns byte, short and char operands into int before any arithmetic operator runs.
a + b has type int even though both operands are bytes, and b and a are not constants, so the sum needs an explicit cast. A cast binds tighter than +, so it only narrows the whole sum when the sum is in parentheses. Every other form casts an operand, which is then promoted back to int by the addition.
- Note that b and a are bytes but not final constants.
- Apply promotion: byte + byte produces an int.
- Check what each cast actually covers; a cast binds tighter than +.
- Only a cast around the whole sum turns the int back into a byte.
Exam tip: byte + byte is an int; cast the whole expression, not the operands.
Java Primitives, Wrapper Classes, Operators and Casting (1Z0-831) — the lesson that teaches this.