Byte class defines static methods to convert string value to byte value

static Byte valueOf(byte b)
Returns a Byte instance representing the specified byte value.
static Byte valueOf(String s)
Returns a Byte object holding the value given by the specified String.
static Byte valueOf(String s, int radix)
Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.
static byte parseByte(String s)
Parses the string argument as a signed decimal byte.

public class Main {
  public static void main(String[] args) {
    System.out.println("parse string to byte:"+Byte.parseByte("10"));
  }
}

The output:


parse string to byte:10
static byte parseByte(String s, int radix)
Parses the string argument as a signed byte in the radix specified by the second argument.

In the following code we convert string 10 based on radix 8.


public class Main {
  public static void main(String[] args) {

    System.out.println("parse string to byte:"+Byte.parseByte("10", 8));
  }
}

The output:


parse string to byte:8

And we use the valueOf methods in the same way:


public class Main {
  public static void main(String[] args) {
    
    System.out.println("parse string to byte:"+Byte.valueOf("10"));
    System.out.println("parse string to byte:"+Byte.valueOf("10", 8));
  }
}

The output:


parse string to byte:10
parse string to byte:8
Home 
  Java Book 
    Essential Classes  

Byte:
  1. Byte
  2. Find out byte's max value, min value and size
  3. Create Byte object with its constructor
  4. Convert Byte to byte, double, float, int, long and short
  5. Decode a string to a byte
  6. Byte class defines static methods to convert string value to byte value
  7. Convert byte value to string value
  8. Compare two byte values