The Byte class simply a wrapper class for the primitive type byte. It wraps the byte primitive value to an object. An object of type Byte contains a single field whose type is byte. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Byte class which is very useful. In addition, this class provides a decent amount of methods for converting a byte to a String and vice versa, as well as other constants and methods useful when dealing with a byte. We will go over each and every method available to Byte class thoroughly.

Byte Class Syntax

public final class Byte
extends Number
implements Comparable<Byte>

Byte Compatibility Version

Byte Class is available since JDK 1.1

Byte Basic Usage

The Byte class as part of the java.lang package is one of the classes of the java api that is being widely used. Let’s tackle the basics of using the Byte class. Let’s discuss first how to instantiate a Byte object

Byte value = new Byte(12);

Basically the declaration of a Byte object is almost the same as we are doing in assigning primitive data type Byte. Similarly to Byte primitive the Byte object has only two possible values, which is either true or false. This is a straightforward method to instantiate a Byte object type. Let’s take another way to do this.

Byte value = 12;

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There are two constructor available to Byte class which we can use to instantiate a Byte object:

Constructor Description
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.

 

We already discussed on how to use the Byte(Byte value) in instantiating a Byte object. So basically the other constructors usage is almost the same only in the other case, the input is in String format.

Byte Test of Equality

Normally when we do equality test for Byte primitive data type we do like this:

		Byte b1= 12;
		byte b2 = 12;
		if(b1==b2){
			System.out.println("They are equal");
		}
		else{
			System.out.println("Values are not equal");
		}

This will no longer work in dealing with Byte object type. To test equality we need to make use of equals which is a method inherited from Object class.

		Byte val1 = 12;
		Byte val2 = new Byte(12);
		if(val1.equals(val2)){
			System.out.println("They are equal");
		}
		else{
			System.out.println("They are not equal");			
		}

Byte Method Usage Examples

Modifier and Type Method and Description
byte byteValue()
Returns the value of this Byte as a byte.
static int compare(byte x, byte y)
Compares two byte values numerically.
int compareTo(Byte anotherByte)
Compares two Byte objects numerically.
static Byte decode(String nm)
Decodes a String into a Byte.
double doubleValue()
Returns the value of this Byte as a double after a widening primitive conversion.
boolean equals(Object obj)
Compares this object to the specified object.
float floatValue()
Returns the value of this Byte as a float after a widening primitive conversion.
int hashCode()
Returns a hash code for this Byte; equal to the result of invoking intValue().
static int hashCode(byte value)
Returns a hash code for a byte value; compatible with Byte.hashCode().
int intValue()
Returns the value of this Byte as an int after a widening primitive conversion.
long longValue()
Returns the value of this Byte as a long after a widening primitive conversion.
static byte parseByte(String s)
Parses the string argument as a signed decimal byte.
static byte parseByte(String s, int radix)
Parses the string argument as a signed byte in the radix specified by the second argument.
short shortValue()
Returns the value of this Byte as a short after a widening primitive conversion.
String toString()
Returns a String object representing this Byte’s value.
static String toString(byte b)
Returns a new String object representing the specified byte.
static int toUnsignedInt(byte x)
Converts the argument to an int by an unsigned conversion.
static long toUnsignedLong(byte x)
Converts the argument to a long by an unsigned conversion.
static Byte valueOf(byte b)
Returns a Byte instance representing the specified byte value.
static Byte valueOf(String s)
Returns a Byte object holding the value given by the specified String.
static Byte valueOf(String s, int radix)
Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.