Type casting into byte in Java

The byte type is encoded on 8 bits, so it takes its values between -128 and 127. In your case, casting by byte is the same as computing a modulo and rounding to an int. Try the following code, the output is the same:

int a = 257;
double d = 323.142;
System.out.println(a % 128);
System.out.println((int) d % 128);

In both cases you are doing a narrowing conversion, which may result in loss of information.

  1. Conversion of int to byte

you convert an int whose value is 257 (00000000 00000000 00000001 00000001 in binary) to a byte. Therefore, only the lowest (right) byte of the int is kept. Therefore the result is 00000001 in binary, which is 1.

  1. Conversion of double to byte

This conversion is more complicated.

  • In the first step 323.142 is converted from double to int, so it becomes 323.
  • The second step is the same as the first conversion :

    323 is 00000000 00000000 00000001 01000011 in binary. Converting 323 to byte keeps the lowest (right) byte, which gives you 67.

Here's what the JLS says about this conversion :

A narrowing conversion of a floating-point number to an integral type T takes two steps:

  1. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

    • If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

    • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

      a. If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

      b. Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

    • Otherwise, one of the following two cases must be true:

      a. The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

      b. The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

  2. In the second step:

    • If T is int or long, the result of the conversion is the result of the first step.

    • If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.


byte b;
int a=257;
double d= 323.142
b=(byte)a; // 257-256=1
  System.out.println(b); // now b is 1
b=(byte)d; // 323.142-256=67
  System.out.println(b); // now b is 67

byte data type is an 8-bit signed two's complement integer(This is important in second case, why 67.142 become 67). byte in Java is signed, so it has a range -2^7 to 2^7-1 - that is, -128 to 127. Since 257 is above 127, you end up wrapping around to 257-256=1. That is 256 is added or subtracted until it falls into range.Same scenario happen in the second case too.