What will be the output for the below code?
public class Test{
public static void main(String[] args){
byte i = 128;
System.out.println(i);
}
}
public class Test{
public static void main(String[] args){
byte i = 128;
System.out.println(i);
}
}
A. 128
B. 0
C. Compilation fails with an error at line 3
D. Compilation fails with an error at line 4
E. None of these
Answer: Option C
Solution (By Examveda Team)
byte can only hold up to 127. So compiler complain about possible loss of precision.static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.
static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.
the range of the byte is -128 to 127 if above 127 is their means it give error so plz first check the range of the byte before going to nxt line if the range is above 127 then it give error if u want an output then give the explicit type convertion to that like as
byte i = (byte)128;
system.out println(b);
output : -128
It cannot converted to 0. since its range is -128 to 127
Plz reply
Why it is not converting to 0
But, why can,t hold byte this no. 128.
plz.... reply