Java - Byte valueOf() method



Description

The Java 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.

Declaration

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

public static Byte valueOf(byte b)

Parameters

b − a byte value

Return Value

This method returns a Byte instance representing b.

Exception

NA

Example 1

The following example shows the usage of Byte valueOf(byte) method. We've created a byte variable and assigned it a value. Then a string variable is created and using Byte.valueOf(byte) method, the value of the byte value is printed.

package com.tutorialspoint;
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 );
   }
}

Output

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

Byte value of byte primitive 20 is 20

Example 2

The following example shows the usage of Byte valueOf(byte) method. We've created a byte variable and assigned it a negative value. Then a string variable is created and using Byte.valueOf(byte) method, the value of the byte value is printed.

package com.tutorialspoint;
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 );
   }
}

Output

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

Byte value of byte primitive -20 is -20

Example 3

The following example shows the usage of Byte valueOf(byte) method. We've created a byte variable and assigned it a zero value. Then a string variable is created and using Byte.valueOf(byte) method, the value of the byte value is printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a byte primitive bt and asign value
      byte bt = 0;

      // 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 );
   }
}

Output

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

Byte value of byte primitive 0 is 0
java_lang_boolean.htm
Advertisements