Compare two byte values

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


public int compareTo(Byte anotherByte)

ReturnsMeaning
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.