Multiplying two bytes

后端 未结 2 2076
孤城傲影
孤城傲影 2021-01-12 22:09

Can somebody explain me why I can\'t to multiply two bytes in this way?

byte a = 1;
byte b = 1;
byte c = a*b;

or

byte a = 1         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 22:53

    Product of two bytes will not necessarily fit into a byte. So Java needs you to tell it that you know what you are doing and confirm that in case of overflow you will get the low-order bits.

提交回复
热议问题