Java.lang Package Classes

Java Byte - valueOf() Method



The java.lang.Byte.valueOf() method 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.

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

Syntax

public static Byte valueOf(String s)
                    throws NumberFormatException

Parameters

s Specify the string to be parsed.

Return Value

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

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.

import java.lang.*;

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

    //creating Byte object
    Byte y = Byte.valueOf(x);

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

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

The output of the above code will be:

The string is: 25
The Byte object is: 25

❮ Java.lang - Byte