Java - Byte valueOf() Method
Description
The Java Byte valueOf(String s) returns a Byte object holding the value given by the specified String. The argument is interpreted as representing a signed decimal byte, exactly as if the argument were given to the parseByte(java.lang.String) method.
The result is a Byte object that represents the byte value specified by the string.
Declaration
Following is the declaration for java.lang.Byte.valueOf() method
public static Byte valueOf(String s) throws NumberFormatException
Parameters
s − the string to be parsed
Return Value
This method returns a Byte object holding the value represented by the string argument.
Exception
NumberFormatException − If the String does not contain a parsable byte.
Parsing a String with plus symbol to get Bytes Example
The following example shows the usage of Byte valueOf(String) method. We've created a String variable and assigned it a value. Then a Byte variable is created and using Byte.valueOf(String) method, the value of the string is parsed and printed.
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "+120";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
Output
Let us compile and run the above program, this will produce the following result −
Byte value of string +120 is 120
Parsing a String with minus symbol to get Bytes Example
The following example shows the usage of Byte valueOf(String) method. We've created a String variable and assigned it a negative value. Then a Byte variable is created and using Byte.valueOf(String) method, the value of the string is parsed and printed.
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "-120";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
Output
Let us compile and run the above program, this will produce the following result −
Byte value of string -120 is -120
Checking NumberFormatException while Parsing a String to get Bytes Example
The following example shows the usage of Byte valueOf(String) method. We've created a String variable and assigned it a invalid value. Then a Byte variable is created and using Byte.valueOf(String) method, the value of the string is parsed and as expected a NumberFormatException occured.
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "-1204";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
Output
Let us compile and run the above program, this will produce the following result −
Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"-1204" Radix:10 at java.base/java.lang.Byte.parseByte(Byte.java:154) at java.base/java.lang.Byte.valueOf(Byte.java:208) at java.base/java.lang.Byte.valueOf(Byte.java:234) at com.tutorialspoint.ByteDemo.main(ByteDemo.java:17)