Java Data Type Tutorial - Byte compareTo(Byte anotherByte) example








Byte compareTo(Byte anotherByte) compares two Byte objects.

Syntax

compareTo has the following syntax.

public int compareTo(Byte anotherByte)

Specified by

compareTo in interface Comparable<Byte>

Parameters

compareTo has the following parameters.

anotherByte
the Byte to compare.

Return

compareTo returns

  • value 0 if this Byte is equal to the passed in Byte;
  • a value less than 0 if this Byte is less than the passed in Byte;
  • a value greater than 0 if this Byte is greater than the passed in Byte.




Example

In the following code shows how to use Byte.compareTo(Byte anotherByte) method.

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: