What is binary numeric promotion in Java, and what type results when you add a byte and a short?
answer
- Two operands -> one common type
- Smallest result type is int
- byte/short/char always -> int
- double > float > long > int precedence
- JVM has no byte/short add bytecode
basics
~10 sWhen you do arithmetic on two numbers, Java converts both to a common type before computing. Anything smaller than int (byte, short, char) becomes int first. So byte + short gives an int.
solid answer
~40 sBinary numeric promotion is the rule Java applies to the two operands of most arithmetic and comparison operators (+, -, *, /, %, <, ==, etc.). Both operands are converted to one common type, and the result has that type. The smallest type the result can ever be is int: byte, short, and char are always widened to int. Beyond that, if either operand is double the other becomes double; else if either is float both become float; else if either is long both become long; otherwise both are int. So byte + short produces an int, even though both inputs are smaller. This is why code like 'byte c = a + b;' fails to compile without a cast: the right-hand side is an int and may not fit in a byte.
code
java · 9 linesbyte a = 10;
short b = 20;
int sum = a + b; // OK: a and b promoted to int, result int
// byte c = a + b; // ERROR: int not assignable to byte
byte c = (byte)(a + b); // OK: explicit narrowing cast
char x = 'A'; // 65
int code = x + 1; // 66 (char promoted to int)go deeper
Knows that small types become int in arithmetic, so 'byte c = a + b;' needs a cast.
Can recite the full double/float/long/int precedence order and explain why the result of byte+short is int.
Ties the rule to the JLS conversion ladder and the JVM's lack of sub-int arithmetic bytecodes; distinguishes it from compound-assignment's implicit cast.
Can reason about how this interacts with autoboxing, overload resolution, and overflow, and explain the design rationale for the int floor in the type system.
## What 'numeric promotion' means Java has primitive number types of different sizes: `byte` (8-bit signed), `short` (16-bit signed), `char` (16-bit unsigned), `int` (32-bit signed), `long` (64-bit signed), `float` (32-bit floating point), and `double` (64-bit floating point). The CPU and the Java bytecode arithmetic instructions operate mainly on `int`, `long`, `float`, and `double`. To compute an arithmetic expression, Java must first convert the operands into one of those types — that conversion is called **numeric promotion**. **Widening conversion** means converting a value to a *bigger* type (e.g. `int` -> `long`); it is always safe and happens automatically because every value of the smaller type fits in the larger one. The reverse (`long` -> `int`) is **narrowing**, may lose information, and requires an explicit cast like `(int)`. ## Binary numeric promotion 'Binary' here means *two operands* (the operator has a left and a right side), not 'base-2'. Binary numeric promotion applies to operators that take two numeric operands: the arithmetic operators `+ - * / %`, the comparison operators `< <= > >=`, the equality operators `== !=` (when both sides are numeric), the bitwise operators `& | ^`, and the conditional `?:` when both branches are numeric. The rule (from the Java Language Specification) is applied in order: 1. If either operand is `double`, the other is converted to `double`. 2. Otherwise, if either operand is `float`, the other is converted to `float`. 3. Otherwise, if either operand is `long`, the other is converted to `long`. 4. Otherwise, both operands are converted to `int`. Step 4 is the key surprise: types **smaller than int** (`byte`, `short`, `char`) never stay small. They are *always* promoted to `int` before the operation, and the result type is `int`. ## Worked example ```java byte a = 10; short b = 20; // a + b: both promoted to int, result is int int sum = a + b; // OK byte bad = a + b; // COMPILE ERROR: result is int, may not fit in byte byte ok = (byte)(a + b); // OK: explicit narrowing cast ``` Even `byte x = 1; x = x + 1;` fails to compile, because `x + 1` is an `int`. (Interestingly `x += 1` works, because compound assignment has a built-in implicit cast.) ## Why it exists The JVM has no dedicated bytecode for `byte` or `short` arithmetic — it only has `iadd`, `ladd`, `fadd`, `dadd`, etc. So small integer types must be promoted to `int` to be added at all. The compiler simply makes this rule explicit and type-safe. With this you can derive the answer to any case: `byte + short` -> both are smaller than int -> rule 4 -> both become `int` -> result is `int`.
- Why does 'byte b = 1; b = b + 1;' fail to compile but 'b += 1;' succeeds?'b + 1' undergoes binary numeric promotion to int, so the right side is an int that cannot be assigned to a byte without a cast. Compound assignment operators (+=, -=, etc.) include an implicit narrowing cast back to the variable's type, so 'b += 1' is treated as 'b = (byte)(b + 1)'.
- What is the result type of 'char + char'?int. Both char operands are promoted to int, so adding two chars yields an int (e.g. 'a' + 'b' is 195, not a char).
saying these in an interview costs you the question
- Saying byte + byte stays a byte
- Thinking the result keeps the larger of the two input sizes (e.g. short + byte = short)
- Believing 'binary' means base-2 rather than two-operand
- Claiming no conversion happens for same-type small operands