This example shows how to convert string to byte in Java the parseByte method of the Byte wrapper class including out of the range values.
How to convert String to byte in Java?
To convert string to byte, use the parseByte
method of the Byte wrapper class.
1 |
public static byte parseByte(String strByteNumber) |
This method returns a byte primitive value by parsing the input string value.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.basic; public class StringToByteExample { public static void main(String[] args){ String strByteNumber = "23"; byte b = Byte.parseByte(strByteNumber); System.out.println("String to byte: " + b); } } |
Output
1 |
String to byte: 23 |
Important Note:
All the characters in the string must be digits, except for the first character of the string which can be a “-” (minus sign) to indicate the negative numeric value.
If any of the characters in the string (except for the first character, if it is a minus sign) is a non-digit character, the NumberFormatException is thrown while converting.
1 2 |
String strByteNumber = "23.43"; byte b = Byte.parseByte(strByteNumber); |
Output
1 2 3 4 5 6 |
Exception in thread "main" java.lang.NumberFormatException: For input string: "23.43" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Byte.parseByte(Unknown Source) at java.lang.Byte.parseByte(Unknown Source) at com.javacodeexamples.basic.StringToByteExample.main(StringToByteExample.java:14) |
Here are some of the example string values and their possible byte outputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//32 System.out.println( Byte.parseByte("32") ); //NumberFormatException System.out.println( Byte.parseByte("32.12") ); //-43 System.out.println( Byte.parseByte("-43") ); //NumberFormatException System.out.println( Byte.parseByte("32a") ); //remove leading zeros, 12 System.out.println( Byte.parseByte("00012") ); //NumberFormatException in older version of Java, 43 in newer versions System.out.println( Byte.parseByte("+43") ); /* * Empty string is not allowed, * NumberFormatException is thrown for input String: "" */ System.out.println( Byte.parseByte("") ); |
Please note that the “+” (plus sign) is allowed as the first character of the String in newer versions of Java. In older versions, if the string has a plus sign as a first character, the parseByte
method throws the NumberFormatException.
Java byte data type range
The byte data type in Java is an 8 bit signed integer value. Range of the byte variable in Java is -128 to 127 (both inclusive). So the byte variable can have a minimum value of -128 while the maximum value it can hold is 127.
What happens when a string contains a number that is not in the range of byte?
When a string object contains a number that is not in the range of the byte data type and when we try to convert, the parseByte
throws the NumberFormatException with a message saying that value is out of range as given below.
1 |
System.out.println( Byte.parseByte("128") ); |
Output
1 2 3 4 |
Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"128" Radix:10 at java.lang.Byte.parseByte(Unknown Source) at java.lang.Byte.parseByte(Unknown Source) at com.javacodeexamples.basic.StringToByteExample.main(StringToByteExample.java:17) |
This example is a part of the Java Basic Examples and Java Type conversion Tutorial.
Please let me know your views in the comments section below.