Compare two byte values

We use compareTo(Byte anotherByte) to compare two Byte objects numerically


public int compareTo(Byte anotherByte)
ValueMeanings
0if this Byte is equal to the argument Byte;
less than 0if this Byte is numerically less than the argument Byte;
greater than 0if this Byte is numerically greater than the argument Byte (signed comparison).

public class Main {
  public static void main(String[] args) {
    
    Byte byte1 = new Byte("1");
    Byte byte2 = new Byte("2");
    System.out.println(byte1.compareTo(byte2));
  }
}

The output:


-1

equals(Object obj) compares this object to the specified object. The result is true if and only if the argument is not null and is a Byte object that contains the same byte value as this object.


public class Main {
  public static void main(String[] args) {
    
    Byte byte1 = new Byte("1");
    Byte byte2 = new Byte("2");
    System.out.println(byte1.equals(byte2));
  }
}

The output:


false
Home 
  Java Book 
    Essential Classes  

Byte:
  1. Byte
  2. Find out byte's max value, min value and size
  3. Create Byte object with its constructor
  4. Convert Byte to byte, double, float, int, long and short
  5. Decode a string to a byte
  6. Byte class defines static methods to convert string value to byte value
  7. Convert byte value to string value
  8. Compare two byte values