Type Promotion in Expressions
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
byte,shortandcharalways becomeintbefore arithmetic.- If the operands still differ, the widest type wins, in this order:
int→long→float→double.
| Expression | Result type | Why |
|---|---|---|
byte + byte | int | Rule 1 |
char + int | int | Rule 1 |
int + long | long | Rule 2: long is wider |
long + float | float | Rule 2 |
int * double | double | Rule 2 |
See It in Code
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);
}
}byte + byte = 30
7 / 2 = 3
7 / 2.0 = 3.5
'A' + 1 = 66
Seats: 936000How it works
- Line 5:
a + bis anint, so it's stored in anint. Storing it in abytewould fail, as shown below. - Line 7: both sides are
int, so the division is whole-number division: 3, with the remainder dropped. - Line 8:
2.0is adouble, so7is promoted and the result is3.5. - Line 9:
'A'is promoted toint65, so the result is the number 66, not the letter B. - Line 13:
int * longis along.
Try it yourself: change line 5 to byte sum = a + b;. Read the error, then fix it with a cast.
Why byte + byte Fails
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 tobyte.
Promotion Happens Step by Step
Java promotes per operation, from left to right, not for the whole expression at once:
| Expression | Steps | Result |
|---|---|---|
1 / 2 * 4.0 | 1 / 2 is int 0, then 0 * 4.0 | 0.0 |
1 * 4.0 / 2 | 1 * 4.0 is 4.0, then 4.0 / 2 | 2.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
- Easy: Write the type and value of each:
5 / 2,5 / 2.0,5.0f / 2,'a' + 0,10L * 3. - Harder: A student scored 437 out of 500. Print the percentage correctly as
87.4, using onlyintvariables for the marks.
Key Takeaways
Was this tutorial helpful?