0

Why does this code give an error if a byte contains -128 and 127 characters?

Byte a = 5; Byte b = 10; Byte c = a+b

18th Mar 2019, 5:15 AM
BestMordeEUW
BestMordeEUW - avatar
2 ответов
+ 2
The problem occurs due to the fact that there is no instruction set to perform operation on a byte type. byte a = 5; // variable a of byte type created 5 assigned to it. byte b = 10; // variable b of byte type created 10 assigned to it. byte c = a+b; // a+b is automatically promoted to integer, // since now result of summary is integer you cannot assign integer to byte. // This occurs as the range of int is much greater than the range of byte In order to bypass this error use casting as follow : byte c = (byte) (a+b);
18th Mar 2019, 6:53 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
0
why does it automatically promoted to integer? 15 is still in a range of a bye. i understand why it creates the error but not why it makes it an integer of it
18th Mar 2019, 2:15 PM
BestMordeEUW
BestMordeEUW - avatar