34
public class Java{
    public static void main(String[] args){
        final byte x = 1;
        final byte y = 2;
        byte z = x + y;//ok
        System.out.println(z);

        byte a = 1;
        byte b = 2;
        byte c = a + b; //Compiler error
        System.out.println(c);
    }
}

If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte.

Why does it happen when we add two final bytes that fit in a byte? There is no compiler error.

Joe
  • 7,749
  • 19
  • 60
  • 110

3 Answers3

34

From the JLS 5.2 Assignment Conversion

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: - A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

In short the value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable that is byte.

Consider your expression

 final byte x = 1;
 final byte y = 2;
 byte z = x + y;//This is constant expression and value is known at compile time

So as summation fits into byte it does not raise an compilation error.

Now if you do

final byte x = 100;
final byte y = 100;
byte z = x + y;// Compilation error it no longer fits in byte
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • So, in summary, the OP is not seeing an error because he fortunately chose values which would sum to fit within a byte. – Duncan Jones Oct 27 '12 at 12:09
  • @Aubin Thanks Added the summary and detail description – Amit Deshpande Oct 27 '12 at 12:18
  • 2
    But adding two int types is allowed. Even in case of int types, overflow may happen right? Why behavior is different with int and byte types? – Rajesh Dec 06 '17 at 05:31
  • 1
    Also, ["A compile-time *constant expression* is an expression \[…\] composed using only the following: • Literals of primitive type and literals of type `String`. \[…\] • The additive operators `+` and `-`. \[…\] • Simple names that refer to constant variables."](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28) Where ["A variable of primitive type or type `String`, that is `final` and initialized with a compile-time constant expression, is called a *constant variable*."](https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4). – Radiodef Jul 11 '18 at 17:07
  • 1
    @Rajesh because binary operation (+) in java would return integer. You may need to cast explicitly byte c = (byte) (a + b); or to declare these byte fields final. However, the += operator does the cast for you. Say, if you have byte a = 2, b = 3; then (a += b) gives you 5 as expected, neither explicit cast nor 'final' is needed. – Václav Apr 05 '20 at 11:40
10
byte z = x + y;  // x and y are declared final

Here, since x and y are declared final so the value of expression on the RHS is known at compile time, which is fixed at (1 + 2 = 3) and cannot vary. So, you don't need to typecast it explicitly

byte c = a + b;   // a and b are not declared final

Whereas, in this case, value of a and b are not declared final. So, the value of expression is not known at compile time, rather is evaluated at runtime. So, you need to do an explicit cast.


However, even in the 1st code, if the value of a + b comes out to be outside the range -128 to 127, it will fail to compile.

final byte b = 121;
final byte a = 120;
byte x = a + b;  // This won't compile, as `241` is outside the range of `byte`

final byte b1 = 12;
final byte a1 = 12;
byte x1 = a1 + b1;  // Will Compile. byte can accommodate `24`
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Whenever we are performing any arithmetic operation between two variable a & b the result is always,

max(int, type of a, type of b)

byte a=10;
byte b=20;
byte c=a+b(C.E )

Explanation: as described above max(int, type of a, type of b)

max(int ,byte,byte)

the result is of type: int , found is int but required in byte

so we need to require typecast into byte

    byte a=10;
    byte b=20;
    byte c=(byte) (a+b);
Anuj
  • 51
  • 6