This example shows how to convert String to byte in Java. This example also shows how to convert String to byte primitive using Byte
wrapper class.
How to convert String to byte in Java?
To convert String to byte, use parseByte
method of Java Byte
wrapper class.
1 | public static byte parseByte(String strByteNumber) |
This method returns 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 “-“(minus sign) to indicate the negative byte value.
If any of the character in the String (except for the first character if it is minus sign) is a non-digit character, NumberFormatException
is thrown while converting String to byte value.
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 output.
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, “+” (plus sign) is allowed as a first character of the String in newer versions of Java. In older versions, if String has plus sign as a first character, parseByte
method throws 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 minimum value of -128 while the maximum value it can hold is 127.
What happens when the String contains a number not in range of byte?
When a String object contains number which is not in range of the byte data type and when we try to convert String to byte, parseByte
throws 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) |
Please let us know your views in the comments section below.
Add Comment