Java.lang Package Classes

Java Byte - valueOf() Method



The java.lang.Byte.valueOf() method returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed byte in the radix specified by the second argument, exactly as if the argument were given to the parseByte(java.lang.String, int) method. The result is a Byte object that represents the byte value specified by the string.

In other words, this method returns a Byte object equal to the value of: new Byte(Byte.parseByte(s, radix)).

Syntax

public static Byte valueOf(String s,
                           int radix)
                    throws NumberFormatException

Parameters

s Specify the string to be parsed.
radix Specify the radix to be used in interpreting s.

Return Value

Returns a Byte object holding the value represented by the string argument in the specified radix.

Exception

Throws NumberFormatException, if the String does not contain a parsable byte.

Example:

In the example below, the java.lang.Byte.valueOf() method returns a Byte object holding the value given by the specified String and parsed with the specified radix.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding byte value
    String x = "100";
    String y = "6F";

    //creating Byte object using radix as 2 (binary)
    Byte p = Byte.valueOf(x, 2);

    //creating Byte object using radix as 16 (hexadecimal)
    Byte q = Byte.valueOf(y, 16);

    //printing the string
    System.out.println("The string x is: " + x); 
    System.out.println("The string y is: " + y); 

    //printing the Byte object 
    System.out.println("The Byte object p is: " + p);   
    System.out.println("The Byte object q is: " + q);    
  }
}

The output of the above code will be:

The string x is: 100
The string y is: 6F
The Byte object p is: 4
The Byte object q is: 111

❮ Java.lang - Byte