Byte datatype - Java

A simple Java program to explain the limitations of byte datatype.
Write a console program to define and initialize a variable of type byte to 1, and then successively multiply it by 2 and display its value 8 times. Explain the reason for the last result.


import java.util.Scanner;

class firstappagain
{
 public static void main(String args[])
 {
  Scanner s = new Scanner(System.in);
  System.out.print("\nInsert a byte value :: ");
  byte b = s.nextByte();
  System.out.print("\nYou inserted :: "+b+"\n");
  
  for(int i=0; i<8; i++)
  {
   b *= 2;
   System.out.print("\nIteration : "+i+"\t Value of B : "+b);
  }
 }
} 

Post a Comment