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:

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.

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:

byte value= Byte.parseByte("12");
System.out.println(value);

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:

System.out.println(Byte.parseByte)("A"));

Running the above code would result to:

Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Byte.parseByte(Byte.java:149)
	at java.lang.Byte.parseByte(Byte.java:175)
	at com.javatutorialhq.java.examples.Test.main(Test.java:7)


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:

Byte.MAX_VALUE
Byte.MIN_VALUE

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

System.out.println(Byte.parseByte("1723"));

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(Byte.java:151)
	at java.lang.Byte.parseByte(Byte.java:175)
	at com.javatutorialhq.java.examples.Test.main(Test.java:7)

Java Byte parseByte(String s) Example

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

package com.javatutorialhq.java.examples;

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(System.in);
		String value = s.nextLine();
		s.close();

		// 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");
		}
		else{
			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