skip to content

Numeric Promotion Rules

In arithmetic, operands are promoted to a common type and anything smaller than int becomes int, which is why adding two bytes yields an int. Interviewers use char arithmetic to make the rule visible.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is binary numeric promotion in Java, and what type results when you add a byte and a short?

level: juniorimportance: must knowfreq 55%

answer

  1. Two operands -> one common type
  2. Smallest result type is int
  3. byte/short/char always -> int
  4. double > float > long > int precedence
  5. JVM has no byte/short add bytecode

basics

~10 s

When 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 s

Binary 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 lines
java
byte 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

for a junior

Knows that small types become int in arithmetic, so 'byte c = a + b;' needs a cast.

for a middle

Can recite the full double/float/long/int precedence order and explain why the result of byte+short is int.

for a senior

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.

for a principal

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

context

open as a page

What surprising results can binary numeric promotion cause in mixed int/long/float/double arithmetic, and how do you avoid them?

level: middleimportance: must knowfreq 52%

basics

~20 s

Because both operands are promoted to a common type, an int/int division stays integer (truncates), and an int times int can overflow before being assigned to a long. Cast one operand to long or double first to fix it.

open as a page

How are char values treated in arithmetic expressions, and what does ''a' + 1' evaluate to?

level: juniorimportance: should knowfreq 48%

basics

~10 s

In arithmetic, a char is treated as its numeric Unicode code value and promoted to int. 'a' has code 97, so 'a' + 1 evaluates to the int 98, not the character 'b'.

open as a page

What is unary numeric promotion, and where does it differ from binary numeric promotion?

level: middleimportance: should knowfreq 40%

basics

~20 s

Unary numeric promotion converts a single operand. If it is smaller than int (byte, short, char), it becomes int; otherwise it keeps its type. It applies to things like unary minus, the array index, bit shifts, and ~.

open as a page

How does numeric promotion interact with method overload resolution and autoboxing when the compiler chooses which overloaded method to call?

level: seniorimportance: should knowfreq 35%

basics

~20 s

When picking an overloaded method, Java first tries widening primitives (like numeric promotion), then boxing, then varargs — in that order. So for a short argument it prefers a method taking int (widening) over one taking Integer (boxing).

open as a page