Java.lang.Byte.valueOf() Method


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


Following is the declaration for java.lang.Byte.valueOf() method

public static Byte valueOf(byte b)


b − a byte value

Return Value

This method returns a Byte instance representing b.




The following example shows the usage of lang.Byte.valueOf() method.

package com.tutorialspoint;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create a byte primitive bt and asign value
      byte bt = -20;

      // create a Byte object b
      Byte b;

       *  static method is called using class name.
       *  assign Byte instance value of bt to b
      b = Byte.valueOf(bt);

      String str = "Byte value of byte primitive " + bt + " is " + b;

      // print b value
      System.out.println( str );

Let us compile and run the above program, this will produce the following result −

Byte value of byte primitive -20 is -20