Skip to Content

  1. /*
  2.   Java Byte Example
  3.   This example shows how an object of Byte can be declared and used.
  4.   Byte is a wrapper class provided to wrap byte primitive value. It has a single
  5.   field of type byte.
  6. */
  7.  
  8. public class ByteExample {
  9.  
  10.   public static void main(String[] args) {
  11.     //create a Byte object using one of the below given constructors
  12.     //1. Create a Byte object from byte
  13.     byte b = 10;
  14.     Byte bObj1 = new Byte(b);
  15.    
  16.     /*
  17.     2. Create Byte object from String. Please note that this method can
  18.     throw NumberFormatException if string doesnt contain parsable number.
  19.     */
  20.     Byte bObj2 = new Byte("4");
  21.    
  22.     //print value of Byte objects
  23.     System.out.println(bObj1);
  24.     System.out.println(bObj2);
  25.   }
  26. }
  27.  
  28.  
  29. /*
  30. Output of the program would be
  31. 10
  32. 4
  33. */