Skip to Content

Convert String to primitive byte Example

  1. /*
  2.   Convert String to primitive byte Example
  3.   This example shows how a String object can be converted into byte primitive type
  4.   using parseByte method of Byte class.
  5. */
  6.  
  7. public class StringToPrimitiveByteExample {
  8.  
  9.   public static void main(String[] args) {
  10.     //declare String object
  11.     String str = new String("10");
  12.    
  13.     /*
  14.     use parseInt method of Byte class to convert String into byte primitive
  15.     data type. This is a static method.
  16.     Please note that this method can throw a NumberFormatException if the string
  17.     is not parsable to byte.
  18.     */
  19.     byte b = Byte.parseByte(str);
  20.     System.out.println(b);
  21.   }
  22. }
  23.  
  24. /*
  25. Output the program would be
  26. 10
  27. */