java.lang.Byte parseByte(String s)


The Byte.parseByte(String s) java method is used primarily in parsing a String method argument into a Byte object. The Byte object is a wrapper class for the byte primitive data type of java API.  I am suggesting that we should have a mastery on this method because it is one of the most used method of Byte class. In my extensive experience of java programming, I have encountered so many instances that I have to convert a String object to another data type. The Byte.parseByte method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Byte.

Make a note that the parseByte method of Byte class is static thus it should be accessed statically which means the we would be calling this method in this format:

Byte.ParseByte(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseByte method non statically.

Important Notes:

  • The method parseByte(String s) throws a NumberFormatException if the string does not contain a parsable byte.

Method Syntax

public static byte parseByte(String s)
throws NumberFormatException

Method Argument

Data Type Parameter Description
String s a String containing the byte representation to be parsed

Method Returns

The parseByte(String s) method of Byte class returns the byte value represented by the argument in decimal.


Requires Java 1.1 and up


 The method parseByte(String s) just parse the String input s and eventually return a byte. As you would have noticed, the String method argument should be a number which means it should be consists of characters that belongs to counting numbers. It is understood that the string input should be consisting of decimal numbers only otherwise invoking the Byte.praseByte() method would throw a NumberFormatException.Let’s take the following code snippet:

byte value= Byte.parseByte("12");

The above code result will give an output of 12. As you would have noticed, we just convert the string “102” into a byte using the static method Byte.parseByte(). You might be wondering what if we put a letter or any other characters beside numbers. The method parseByte would throw NumberFormatException. Like for this example:


Running the above code would result to:

Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
	at java.lang.NumberFormatException.forInputString(
	at java.lang.Integer.parseInt(
	at java.lang.Byte.parseByte(
	at java.lang.Byte.parseByte(

The above exception will be encountered if the input is not a recognizable value by Byte class. You might want to surround the statement with a try-catch block and handle this type of exception.

Java Byte.parseByte maximum and minimum value

There is always a limit on what we could parse because of the limitation of the byte data type with regards to the maximum and minimum value. You might be wondering how to determine the maximum and minimum value that is allowable by the parseByte method. Well getting the threshold is quite easy and can be determine by using the two static values available in Byte class which is the following:


Let’s take an example that crosses the minimum and maximum value that we could parse using the Byte.parseByte java method:


Running the above code would result to the following:

Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"1723" Radix:10
	at java.lang.Byte.parseByte(
	at java.lang.Byte.parseByte(

Java Byte parseByte(String s) Example

Below is a simple java example on the usage of parseByte(String s) method of Byte class.


import java.util.Scanner;

 * This example source code demonstrates the use of 
 * parseByte(String s) method of Byte class.

public class ByteParseByteStringExample {

	public static void main(String[] args) {

		// ask the age of the user
		System.out.print("How old are you?");

		// read the user input
		Scanner s = new Scanner(;
		String value = s.nextLine();

		// convert the user input into byte
		byte age = Byte.parseByte(value);

		 * block A, for user < 10 		 * block B, for user >=10

		if(age == 10 || age>10){
			System.out.print("You are on Block A");
		else if(age0 ) {
			System.out.print("You are on Block B");
			System.out.print("Invalid Input");


Sample Output

Below is the sample output when you run the above example.

java Byte parseByte(String s) example output