How are integers cast to bytes in Java?

后端 未结 8 1177
我在风中等你
我在风中等你 2020-11-27 21:02

I know Java doesn\'t allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and I cast the integer to a byte

相关标签:
8条回答
  • 2020-11-27 21:42

    According to my understanding, you meant

    Integer i=new Integer(2);
    byte b=i;   //will not work 
    
    final int i=2;
    byte b=i;   //fine 
    

    At last

    Byte b=new Byte(2);
    int a=b;   //fine
    
    0 讨论(0)
  • 2020-11-27 21:44

    or does it just directly copy the last 8 bits of the integer

    yes, this is the way this casting works

    0 讨论(0)
提交回复
热议问题