Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

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.

like image 244
Joe Avatar asked Oct 27 '12 12:10

Joe


People also ask

Can we add byte and int?

The byte data type in Java is a signed integer based on the two's complement 8-bit mechanism. It is different from the int data type that uses 4 bytes (i.e., 32-bit to store a number). The values that can be stored in a single byte are -128 to 127.

Can we assign int to byte?

You can use 256 values in a byte, the default range is -128 to 127, but it can represent any 256 values with some translation. In your case all you need do is follow the suggestion of masking the bits. int val =233; byte b = (byte)val; System.


2 Answers

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 
like image 138
Amit Deshpande Avatar answered Sep 18 '22 17:09

Amit Deshpande


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`
like image 20
Rohit Jain Avatar answered Sep 21 '22 17:09

Rohit Jain