java.lang.Byte parseByte(String s)

Description

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:

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.

Compatibility

Requires Java 1.1 and up

Discussion

 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:

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:


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: