I don’t understand the result of double-type byte conversion

Hello, I had a 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 the result:

i and b 257 1

d and i 323.142 323

d and b 323.142 67

I understand why the result of the first conversion is 1, and I also understand the second conversion, but I'm not sure why this is the result of 67 at the last conversion, I can’t understand this, so I need your help. Thanks

+3
source share
1 answer

All of them are called Primitive Transformation Constraint (§5.1.3) :

  • Conversation (byte):

           257 = 0000 0001 0000 0001
    

    Truncating the top byte gives:

    (byte) 257 = xxxx xxxx 0000 0001
    

    which is obviously 1.

  • Floating-point interactions are always rounded to zero .

  • double byte :

    • double int, round to zero.

      (int) 323.142  ~~~>  323
      
    • int .

      (byte) 323     ~~~~> 67
      
             323 = 0000 0001 0100 0011
      (byte) 323 = xxxx xxxx 0100 0011
                 = 67
      
+5

All Articles