0

Why getting compile error "cannot convert from ing to byte " in this code ? :-

byte a = 5; byte b = 5; byte c = a+b;

13th Feb 2018, 5:48 AM
Prashant Kumar
Prashant Kumar - avatar
3 Respuestas
+ 3
byte a = 5; byte b = 5; byte c = (byte )(a+b); System.out.println(c); while adding 2 byte variable java promotes them to int... a+b is a int type though...a,b are individually byte variable.. you need to convert the sum again into byte... like the third line...
13th Feb 2018, 5:58 AM
sayan chandra
sayan chandra - avatar
+ 1
Instead of "byte" you need to put "int"
13th Feb 2018, 6:19 AM
Merer
Merer - avatar
+ 1
You need specifically typecast it to byte In Java, every non floating number is by default of int type. So when you added two byte variables (a+b) then it became a int value. To overcome it, just typecast it like this byte c = (byte)(a+b);
13th Feb 2018, 11:16 AM
Ankit Saxena
Ankit Saxena - avatar