Home »
Java programming language
Java - Byte Class parseByte() Method
Short class parseByte() method: Here, we are going to learn about the parseByte() method of Short class with its syntax and example.
By Preeti Jain Last updated : March 18, 2024
Syntax
public static byte parseByte(String str);
public static byte parseByte(String str, int radix's);
Short class parseByte() method
- parseByte() method is available in java.lang package.
- parseByte(String str) method is used to return the byte value corresponding to the given String denotation or in other words we can say this method is used to convert string value to a byte value.
- parseByte (String str, int radix's) method is used to return the byte value corresponding to the given String denotation as a signed byte in the radix's given by the second argument.
- parseByte(String str), parseByte(String str, int radix's) method may throw a NumberFormatException at the time of conversion from String to byte.
NumberFormatException: In this exception, if String does not contain the parsable number.
- These are static the methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.
Parameters
- In the first case, String value – represents the value of String type.
- In the second case, String value, int radix's – In this method first parameter value represent the value of String type in the radix's given by the second parameter.
Return Value
In the first case, the return type of this method is byte - it returns the byte representation of this String argument.
In the second case, the return type of this method is byte - it returns the byte representation of the String argument in the radix's given by the second argument.
Example
// Java program to demonstrate the example
// of parseByte() method of Byte class
public class ParseByteOfByteClass {
public static void main(String[] args) {
// Variables initialization
String str1 = "100";
String str2 = "67";
int radix = 10;
// Object initialization
Byte b1 = new Byte(str2);
// It convert string into byte by calling parseByte(str1) method
// and store the result in another variable of byte type
byte result = b1.parseByte(str1);
// Display result
System.out.println("b1.parseByte(str1): " + result);
// It convert string into byte with radix 20 by calling parseByte(str1,radix's) method
// and store the result in a variable of byte type
result = b1.parseByte(str1, radix);
// Display result
System.out.println("b1.parseByte(str1,radix): " + result);
}
}
Output
b1.parseByte(str1): 100
b1.parseByte(str1,radix): 100