0

I have a rather simple - but for me confusing - question. Assume the byte variabels a,b,c:

byte a = 5;
byte b = 3;
byte c = a + b; // wont compile

Line 3 wont compile because - I suppose - Java has to make a calculation behind the scenes and the outcome of the calculation is an integer. An integer can't be passed to a byte without explicit casting. So (byte)(a+b) should have been provided. But now assume there is a 4th line of code with an explicit cast to integer ...

c = (int) 8 ; // compiles

It compiles although the byte variabel 'c' is explicitly casted to integer. How is Java handling this aspect ... ?

ridi
  • 162
  • 9
  • Bytes have their own operators. http://www.tutorialspoint.com/java/java_bitwise_operators_examples.htm and they are not integers. – Tschallacka Jun 02 '15 at 11:37
  • Looks like this has been asked before. Have a look [here][1]? [1]: http://stackoverflow.com/questions/18483470/is-addition-of-byte-converts-to-int-is-because-of-java-language-rules-or-because – pjanssen Jun 02 '15 at 11:39
  • Try `c=(int)128` and you might know the difference yourself !! – Neeraj Jain Jun 02 '15 at 11:40

3 Answers3

1

What you are failing to see is that a+b is a run-time operation whereas byte b = (int)8; is a compile-time operation. SO, during compile-time, the compiler knows that the constant 8 can be fit in a byte. So, the explicit conversion to (int) is ignored. The byte code will have not difference with or without the (int).

Now, in the first case if both byte variables were final (i.e, constant), then the compiler would have allowed this :

public static void main(String[] args) {
    final byte a = 5; // constant
    final byte b = 3; // constant
    byte c = a + b; // WILL compile;

}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Line 3 wont compile because - I suppose - Java has to make a calculation behind the scenes and the outcome of the calculation is an integer.

Correct.

But now assume there is a 4th line of code with an explicit cast to integer

c = (int) 8 ; // compiles

It compiles although the byte variabel 'c' is explicitly casted to integer.

Because you used a literal value, 8, and the compiler knows 8 will fit in byte.

A better comparison would be:

c = (int)b;

...which fails.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The point is byte is of 8 bits and int is 32 bits integer value. So for instance, when you add two byte numbers under 127 then that can add to a result which is over 127 which is out of the range of byte, and by default Java uses int for almost all numbers.

The JVM doc says:

A compiler encodes loads of literal values of types byte and short using Java Virtual Machine instructions that sign-extend those values to values of type int at compile-time or run-time. Loads of literal values of types boolean and char are encoded using instructions that zero-extend the literal to a value of type int at compile-time or run-time. [..]. Thus, most operations on values of actual types boolean, byte, char, and short are correctly performed by instructions operating on values of computational type int.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331