Java Data Type How to - Convert String to byte








Question

We would like to know how to convert String to byte.

Answer

// w w w  .ja v a  2 s  .  co m
public class Main {
  public static void main(String[] args) {
    String s = "65";

    byte b = Byte.valueOf(s);

    System.out.println(b);

    // Causes a NumberFormatException since the value is out of range
    System.out.println(Byte.valueOf("129"));
  }
}

The code above generates the following result.