Description

Based from the official documentation of Oracle, the equals method of Byte class compares this Byte object against the specified object method argument. The result is true if and only if the argument is not null and is a Byte object that represents a byte with the same value as the float represented by this object.

Important Notes:

  • The method  equals(Object obj) overrides equals in class Object

Method Syntax

public boolean equals(Object obj)

Method Argument

Data Type Parameter Description
Object obj the object to compare with

Method Returns

The equals(Object obj) method of Byte class returns true if the objects are the same; false otherwise.

Compatibility

Requires Java 1.1 and up

Discussion

Normally in dealing with primitive type byte we test equality by using == operators. This will no longer true if we are dealing with Byte wrapper class because inherently to test two object equality we have to use the equals() method, which is by default being inherited by all java objects from class Object.

Normally if the above code declaration uses byte primitive type, the result would be true however since the byte value 12 is assigned to wrapper class Byte, it would print out false. If we wanted to use the == operator instead of equals, then we need to convert the Byte values using the byteValue() method of Float class so as to convert the wrapper classes to primitive values

The above code snippet would result to true.

Java Byte equals(Object obj) Example

Below is a simple java example on the usage of equals() method of Byte class.

Basically on the above example, we have declared a Byte which has a value 15 and also an object whose type is Byte with a value of 15 as well. To demonstrate the difference in using the operator == and equals() method, we have evaluated both and printed the result. You would notice the == operator will yield to false because both of the objects are two different instances. While the equals() method will yield true because the two Byte have the same numerical value.

Sample Output