- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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