Dont understand the result of conversion of type double to type byte

casting, java, type-conversion

Solution

All of these are called Narrowing Primitive Conversion (§5.1.3):

The conversation to `(byte)`:

       257 = 0000 0001 0000 0001

Truncating the high byte gives:

(byte) 257 = xxxx xxxx 0000 0001

which is obviously 1.

The conversation from a floating point to an integer is always round to zero.

The conversation from `double` to `byte` happens in two steps:

The `double` gets casted to an `int`, following the round to zero rule.

(int) 323.142  ~~~>  323

The `int` gets truncated to a byte.

(byte) 323     ~~~~> 67

       323 = 0000 0001 0100 0011
(byte) 323 = xxxx xxxx 0100 0011
           = 67

Problem

Hello i had task from my book to write this code ``` public class EkspKonverzija { public static void main(String args[]) { byte b; int i=257; double d= 323.142; b=(byte) i; System.out.println("i and b "+i+" "+b); i=(int) d; System.out.println("d and i "+d+" "+i); b=(byte) d; System.out.println("b and d "+b+" "+d); } } ``` And result is: i and b 257 1 d and i 323.142 323 d and b 323.142 67 I understand why the result of first conversion is 1, and i also understand the second conversion, but i dont undetsand why is result 67 at last conversion, i cant figure it out so i need your help. Thanks

Original source