Convert String to byte in Java

Tags: String byte Convert

In this Java core tutorial, we learn how to convert String value into byte value in Java via different solutions.

Using Byte.parseByte() static method to parse String into byte value in Java

ConvertStringToByteExample1.java

public class ConvertStringToByteExample1 {
    public static void main(String[] args) {
        String value1 = "22";
        int value2 = Byte.parseByte(value1);

        System.out.println("String value: " + value1);
        System.out.println("byte value: " + value2);
    }
}
The output is:
String value: 22
byte value: 22

Using Byte.valueOf() method to instantiate Byte object from a given String value

ConvertStringToByteExample2.java

public class ConvertStringToByteExample2 {
    public static void main(String[] args) {
        String value1 = "19";
        byte value2 = Byte.valueOf(value1);

        System.out.println("String value: " + value1);
        System.out.println("byte value: " + value2);
    }
}
The output is:
String value: 19
byte value: 19

Happy Coding 😊