The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is a byte.
In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.
Byte class Constructors
- Byte(byte value) - Constructs a newly allocated Byte object that represents the specified byte value.
- Byte(String s) - Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.
Example:
byte b = 55;
String str = "45";
// Construct two Byte objects
Byte x = new Byte(b);
Byte y = new Byte(str);
Byte class Methods
The class diagram shows a list of APIs/Methods that Byte class provides.
In this post, we will learn a few important methods of Byte wrapper class.
Example: This program demonstrates usage of Byte wrapper class methods.
/**
* This class to demonstrate Byte wrapper class methods
* @author javaguides
*
*/
public class ByteClassExample {
public static void main(String[] args) {
byte b = 55;
String str = "45";
// Construct two Byte objects
Byte x = new Byte(b);
Byte y = new Byte(str);
// toString()
System.out.println("toString(b) = " + Byte.toString(b));
// valueOf()
// return Byte object
Byte z = Byte.valueOf(b);
System.out.println("valueOf(b) = " + z);
z = Byte.valueOf(str);
System.out.println("ValueOf(bb) = " + z);
z = Byte.valueOf(str, 6);
System.out.println("ValueOf(bb,6) = " + z);
// parseByte()
// return primitive byte value
byte zz = Byte.parseByte(str);
System.out.println("parseByte(bb) = " + zz);
zz = Byte.parseByte(str, 6);
System.out.println("parseByte(bb,6) = " + zz);
//decode()
String decimal = "45";
String octal = "005";
String hex = "0x0f";
Byte dec=Byte.decode(decimal);
System.out.println("decode(45) = " + dec);
dec=Byte.decode(octal);
System.out.println("decode(005) = " + dec);
dec=Byte.decode(hex);
System.out.println("decode(0x0f) = " + dec);
System.out.println("bytevalue(x) = " + x.byteValue());
System.out.println("shortvalue(x) = " + x.shortValue());
System.out.println("intvalue(x) = " + x.intValue());
System.out.println("longvalue(x) = " + x.longValue());
System.out.println("doublevalue(x) = " + x.doubleValue());
System.out.println("floatvalue(x) = " + x.floatValue());
int hash=x.hashCode();
System.out.println("hashcode(x) = " + hash);
boolean eq=x.equals(y);
System.out.println("x.equals(y) = " + eq);
int e=Byte.compare(x, y);
System.out.println("compare(x,y) = " + e);
int f=x.compareTo(y);
System.out.println("x.compareTo(y) = " + f);
}
}
Output:
toString(b) = 55
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseByte(bb) = 45
parseByte(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 10
x.compareTo(y) = 10
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course