Why is the sum of bytes integer?

I have a tyo byte variable

byte a = 3;
byte b = 4;

If I sum them up, the value of the sum will be integer.

byte z = a+b  //error, left side is byte, right side is integer

Why a + b is int?

+3
source share
1 answer

Because the Java Language Specification says so

Binary numeric promotion is performed on operands (Β§5.6.2).

Please note that binary numerical advancement performs conversion of a set of values ​​(Clause 5.1.13) and can perform conversion for unpacking (Clause 5.1.8).

The type of additive expression for numeric operands is the advanced type of its operands.

and regarding numerical progress ,

(Β§5.1.2) , :

  • [...]
  • int.

, byte int . , a int.

byte z = (byte) (b + a);

/ .

+8

All Articles