Type Promotion in Expressions

Naveen AnamaneniUpdated · 5 min read

You add two byte values, both small, and try to store the result in a byte. The compiler refuses. That looks like a bug in Java, but it follows a simple rule. Before doing arithmetic, Java promotes the values to a common type, and the result has that type. Learn the two rules below and you'll be able to predict the type of any expression.

The Two Rules

What type does an expression have?
  1. byte, short and char always become int before arithmetic.
  2. If the operands still differ, the widest type wins, in this order: int → long → float → double.
ExpressionResult typeWhy
byte + byteintRule 1
char + intintRule 1
int + longlongRule 2: long is wider
long + floatfloatRule 2
int * doubledoubleRule 2

See It in Code

Promotion.java▶ Try it
public class Promotion {
    public static void main(String[] args) {
        byte a = 10;
        byte b = 20;
        int sum = a + b;
        System.out.println("byte + byte = " + sum);
        System.out.println("7 / 2   = " + (7 / 2));
        System.out.println("7 / 2.0 = " + (7 / 2.0));
        System.out.println("'A' + 1 = " + ('A' + 1));
 
        int seats = 72;
        long trains = 13_000L;
        long totalSeats = seats * trains;
        System.out.println("Seats: " + totalSeats);
    }
}
Output
byte + byte = 30
7 / 2   = 3
7 / 2.0 = 3.5
'A' + 1 = 66
Seats: 936000

How it works

  1. Line 5: a + b is an int, so it's stored in an int. Storing it in a byte would fail, as shown below.
  2. Line 7: both sides are int, so the division is whole-number division: 3, with the remainder dropped.
  3. Line 8: 2.0 is a double, so 7 is promoted and the result is 3.5.
  4. Line 9: 'A' is promoted to int 65, so the result is the number 66, not the letter B.
  5. Line 13: int * long is a long.

Try it yourself: change line 5 to byte sum = a + b;. Read the error, then fix it with a cast.

Why byte + byte Fails

Compiler output
ByteAdd.java:5: error: incompatible types: possible lossy conversion from int to byte
        byte sum = a + b;
                     ^

The result is an int, and putting an int into a byte is narrowing. Two bytes could add up to more than 127, so Java won't do it silently. Cast it if you're sure it fits: byte sum = (byte) (a + b);.

Two exceptions keep everyday code tidy:

  • Constant expressions: byte b = 10 + 20; compiles, because the compiler can see that 30 fits.
  • Compound assignment: b += 5; compiles, because += includes a hidden cast back to byte.

Promotion Happens Step by Step

Java promotes per operation, from left to right, not for the whole expression at once:

ExpressionStepsResult
1 / 2 * 4.01 / 2 is int 0, then 0 * 4.00.0
1 * 4.0 / 21 * 4.0 is 4.0, then 4.0 / 22.0
"Total: " + 2 + 3"Total: 2", then "Total: 23""Total: 23"
2 + 3 + " total"5, then "5 total""5 total"

The last two rows show the same idea for + with text: once a String is involved, + joins instead of adding.

Common Mistakes

Integer division by accident. int marks = 425; double percent = marks / 500 * 100; gives 0.0. Make one side a double first: marks / 500.0 * 100 gives 85.0.

Expecting char + 1 to be a char. It's an int. Cast it back: (char) ('A' + 1).

Overflow before promotion. long ms = 30 * 24 * 60 * 60 * 1000; overflows, because all the numbers are int and the multiplication happens in int. Write 30L * 24 * 60 * 60 * 1000.

Practice

  1. Easy: Write the type and value of each: 5 / 2, 5 / 2.0, 5.0f / 2, 'a' + 0, 10L * 3.
  2. Harder: A student scored 437 out of 500. Print the percentage correctly as 87.4, using only int variables for the marks.

Quick check: Type promotion

4 questions
1What is the type of byte + byte?
2What does 7 / 2 evaluate to?
3What does 'A' + 1 evaluate to?
4Why does byte b = 10; b += 5; compile, but b = b + 5; doesn't?

Key Takeaways

Was this tutorial helpful?

Lesson 39 of 482