Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Why does byte b = 10; b = b + 1; fail to compile while b += 1; and b++ compile?

b + 1 fails because Java promotes byte operands to int before any arithmetic, so the result is an int, and assigning an int to a byte variable needs an explicit cast. b += 1 and b++ compile because the language defines compound assignment and the increment operators with a built-in narrowing cast back to the variable's type. That hidden cast is convenient, but it also hides overflow: a byte holding 127 becomes -128 after b++.

Background to this question

This looks like a trivia question, but it tests whether we know two rules that affect real code: numeric promotion, which decides the type of every arithmetic expression, and the implicit cast inside compound operators, which can silently lose data. Both show up in parsing binary data, in counters and in code reviews. In this answer, we will recap the integer types and what narrowing means, see why b + 1 is an int, look at how the specification defines +=, ++ and their hidden cast, and then show how that cast hides overflow.

1A Quick Recap: Widening and Narrowing

A byte holds 8 bits, from -128 to 127, while an int holds 32 bits. Converting a byte to an int is a widening conversion: every byte value fits, so Java does it automatically. Converting an int to a byte is a narrowing conversion: most int values do not fit, and Java keeps only the lowest 8 bits. Because narrowing can lose information, Java requires us to write the cast ourselves, (byte) x, so that the loss is visible in the code.

2Why b + 1 Is an int

Before applying a binary operator such as +, -, * or /, Java performs binary numeric promotion on the operands. If either operand is double, float or long, both are converted to that type; otherwise both are converted to int. There is no byte or short arithmetic in Java at all. So in b + 1, b is widened to int, 1 is already an int, and the result is the int 11.

Java
byte b = 10;
// b = b + 1; // error: incompatible types: possible lossy conversion from int to byte
b = (byte) (b + 1); // 11: explicit narrowing, allowed
byte x = 1, y = 2;
// byte z = x + y; // same error: x + y is an int, even though both operands are bytes
byte k = 10 + 1; // 11: compiles, because 10 + 1 is a constant that fits in a byte

Notice the last line. Java makes one exception for assignment: when the right-hand side is a compile-time constant expression of type int whose value fits in the target type, the narrowing is allowed without a cast. b + 1 is not a constant because b is a variable, so that exception does not apply.

The reason for the promotion rule is the JVM itself. Its arithmetic instructions work on int, long, float and double; there are no byte or short add instructions. Promoting to int matches what the machine really does.

3Why += and ++ Compile

The Java Language Specification defines a compound assignment E1 op= E2 as equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, and E1 is evaluated only once. In other words, the cast is part of the operator's definition. The prefix and postfix increment and decrement operators are defined the same way: the value is promoted, 1 is added, and the result is narrowed back to the variable's type.

Java
byte b = 10;
b += 1; // means: b = (byte) (b + 1) -> 11
b++; // also narrowed back to byte -> 12
b *= 20; // b = (byte) (12 * 20) = (byte) 240 -> -16
Same arithmetic, different rules
ExpressionArithmetic typeCast back to byteCompiles?
b = b + 1intnoneno
b = (byte) (b + 1)intexplicityes
b += 1intimplicit, from the JLS definitionyes
b++intimplicit, from the JLS definitionyes
byte k = 10 + 1int constantallowed for constants that fityes

So the rule is consistent: the arithmetic is still done in int, but the compound operators include the cast that we would otherwise have to write. The language designers chose this because code such as counter += 1 on a short or a char should not need a cast every time.

4The Hidden Cast Hides Overflow

Because the cast is implicit, compound operators never complain about values that do not fit. They wrap around silently, which can surprise us when a byte or short is used as a counter.

Java
byte b = 127;
b++;
System.out.println(b); // -128: 128 does not fit, so the low 8 bits give -128
short s = 32_767;
s += 1;
System.out.println(s); // -32768
int i = 0;
i += 3.7; // means i = (int) (i + 3.7)
System.out.println(i); // 3: the fraction is silently dropped

The last example is the most dangerous one in practice: += with a double on the right compiles even when the variable is an int, and the fractional part disappears. When a value can exceed the range of its type, it is better to use int or long for the arithmetic and check the range explicitly, or to use Math.addExact, which throws on overflow.

5Follow-up questions

Does the same rule apply to char and short?

Yes. char and short are also promoted to int in arithmetic, so char c = 'a'; c = c + 1; does not compile, while c += 1 and c++ do, giving 'b'. That is why loops over letters often use c++ without a cast.

Why does final byte one = 1; byte r = one + one; compile?

Because one is a constant variable, so one + one is a compile-time constant expression with the value 2, which fits in a byte. The assignment rule for constants then allows the narrowing. If one were not final, the expression would not be constant and the line would fail.

Is byte arithmetic slower than int arithmetic?

Not meaningfully. The JVM does the work in int anyway and then narrows when storing. Bytes are useful for saving memory in large arrays, not for faster arithmetic.

6Conclusion

In this answer, we saw that binary numeric promotion widens byte operands to int, so b + 1 is an int and needs an explicit cast to go back into a byte. Compound assignment is defined as E1 = (T) ((E1) op (E2)), and ++ and -- include the same narrowing, which is why b += 1 and b++ compile. We also saw that this implicit cast hides overflow, so 127 becomes -128, and that i += 3.7 silently drops the fraction.

What to say in the interviewAbout 90 seconds aloud

Open with

The difference comes from two rules in the language specification. The first is binary numeric promotion: before any arithmetic, byte, short and char operands are widened to int, because the JVM only has int, long, float and double arithmetic.

Then, if they want more

So in b = b + 1, the expression b + 1 is an int, and assigning an int to a byte is a narrowing conversion, which needs an explicit cast. The compiler reports a possible lossy conversion. The only exception is a constant expression that fits, so byte k = 10 + 1 compiles, but b is a variable.

The second rule is the definition of compound assignment. E1 op= E2 is defined as E1 = (T)((E1) op (E2)), where T is the type of E1, so b += 1 contains an implicit cast back to byte. The increment and decrement operators are defined the same way, so b++ also narrows.

The catch is that the hidden cast also hides overflow. If b is 127, b++ gives minus 128 with no error, and similarly int i += 3.7 compiles and drops the fraction.

So I treat compound operators on small types with care: if a value can go out of range, I do the arithmetic in int or long and check the range, or use Math.addExact.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does byte b = 10; b = b + 1; fail to compile while b += 1; and b++ compile?": https://scrivio-interview-library.netlify.app/java/types-and-values/c81077de29/