java.lang.Byte parseByte(String s, int radix)

Description

The Byte.parseByte(String s) java method parses the string argument as a signed byte in the radix specified by the second argument. The characters in the string must all be digits, of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value) except that the first character may be an ASCII minus sign ‘-‘ (‘u002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘u002B’) to indicate a positive value. The resulting byte value is returned.

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(String s, int radix)

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 following occurs
    • The first argument is null or is a string of length zero.
    • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
    • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ (‘u002D’) or plus sign ‘+’ (‘u002B’) provided that the string is longer than length 1.
    • The value represented by the string is not a value of type byte.

Method Syntax

public static byte parseByte(String s, int radix)
throws NumberFormatException

Method Argument

Data Type Parameter Description
String s a String containing the byte representation to be parsed
int radix the radix to be used while parsing s

Method Returns

The parseByte(String s, int radix) method of Byte class returns the byte value represented by the string argument in the specified radix.

Compatibility

Requires Java 1.1 and up

Java Byte parseByte(String s, int radix) 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, int radix) method of Byte class.
 */

public class ByteParseByteStringRadixExample {

	public static void main(String[] args) {

		// ask for user input
		System.out.print("Enter a hexadecimal number:");

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

		// convert the user input into Byte 
		Byte result = Byte.parseByte(value,16);

		// print the result
		System.out.print(value +" hex is equal to "+result);
	}

}

Sample Output

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

java Byte parseByte(String s, int radix) example output