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.
Join The Discussion
Comments ( 5 )
Related Questions on Data Types and Variables
What is the maximum value that can be stored in a byte variable in Java?
A. 127
B. 255
C. 32767
D. 64
What is the default value of an int variable in Java if it's not explicitly initialized?
A. 0
B. 1
C. -1
D. Null
Which of the following is not a valid identifier for a Java variable?
A. my_var
B. _myVar
C. 3rdVar
D. $var
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