Java.lang Package Classes

Java Byte - valueOf() Method



The java.lang.Byte.valueOf() method returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.

Syntax

public static Byte valueOf(byte b)

Parameters

b Specify a byte value.

Return Value

Returns a Byte instance representing b.

Exception

NA.

Example:

In the example below, the java.lang.Byte.valueOf() method returns a Byte instance representing the given byte value.

import java.lang.*;

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

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

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

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

The output of the above code will be:

The byte value is: 25
The Byte instance is: 25

❮ Java.lang - Byte

5