Examveda
Examveda

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);
      }
}

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.

This Question Belongs to Java Program >> Data Types And Variables

Join The Discussion

Comments ( 5 )

  1. Mallikarjun Guggari
    Mallikarjun Guggari :
    9 months ago

    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

  2. Examveda
    Examveda :
    7 years ago

    It cannot converted to 0. since its range is -128 to 127

  3. Padmavathi Vaka
    Padmavathi Vaka :
    7 years ago

    Plz reply

  4. Padmavathi Vaka
    Padmavathi Vaka :
    7 years ago

    Why it is not converting to 0

  5. Ankita Maurya
    Ankita Maurya :
    7 years ago

    But, why can,t hold byte this no. 128.
    plz.... reply

Related Questions on Data Types and Variables