0
Why getting compile error "cannot convert from ing to byte " in this code ? :-
byte a = 5; byte b = 5; byte c = a+b;
3 Antworten
+ 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...
+ 1
Instead of "byte" you need to put "int"
+ 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);