java.lang.Byte parseByte(String s)
Description
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
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.