How to Convert a String value to Byte value in Java with Examples
Given a String “str” in Java, the task is to convert this string to byte type.
Examples:
Input: str = "1" Output: 1 Input: str = "3" Output: 3
Approach 1: (Naive Method)
One method is to traverse the string and add the numbers one by one to the byte type. This method is not an efficient approach.
Approach 2: (Using Byte.parseByte() method)
The simplest way to do so is using parseByte() method of Byte class in java.lang package. This method takes the string to be parsed and returns the byte type from it. If not convertible, this method throws error.
Syntax:
Byte.parseByte(str);
Below is the implementation of the above approach:
Example 1: To show successful conversion
// Java Program to convert string to byte class GFG { // Function to convert String to Byte public static byte convertStringToByte(String str) { // Convert string to byte // using parseByte() method return Byte.parseByte(str); } // Driver code public static void main(String[] args) { // The string value String stringValue = "1"; // The expected byte value byte byteValue; // Convert string to byte byteValue = convertStringToByte(stringValue); // Print the expected byte value System.out.println( stringValue + " after converting into byte = " + byteValue); } } |
1 after converting into byte = 1
Approach 3: (Using Byte.valueOf() method)
The valueOf() method of Byte class converts data from its internal form into human-readable form.
Syntax:
Byte.valueOf(str);
Below is the implementation of the above approach:
Example 1: To show successful conversion
// Java Program to convert string to byte class GFG { // Function to convert String to Byte public static byte convertStringToByte(String str) { // Convert string to byte // using valueOf() method return Byte.valueOf(str); } // Driver code public static void main(String[] args) { // The string value String stringValue = "1"; // The expected byte value byte byteValue; // Convert string to byte byteValue = convertStringToByte(stringValue); // Print the expected byte value System.out.println( stringValue + " after converting into byte = " + byteValue); } } |
1 after converting into byte = 1
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- How to Convert a Byte value to String value in Java with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
- How to Convert a Float value to String value in Java with Examples
- How to Convert a Short value to String value in Java with Examples
- How to Convert a Double value to String value in Java with Examples
- How to Convert a String value to Float value in Java with Examples
- How to Convert a String value to Short value in Java with Examples
- Convert byte[] array to File using Java
- Program to convert Byte Array to Writer in Java
- Byte longValue() method in Java with examples
- Byte doubleValue() method in Java with examples
- Byte shortValue() method in Java with examples
- Byte intValue() method in Java with examples
- Byte floatValue() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

