VOOZH about

URL: https://www.geeksforgeeks.org/java/byte-compareto-method-in-java-with-examples/

⇱ Byte compareTo() method in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Byte compareTo() method in Java with examples

Last Updated : 11 Jul, 2025
The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax
ByteObject.compareTo(Byte a)
Parameters: It takes a Byte type object a as input which is to be compared with the instance of the Byte object calling the compareTo method. Return Value: It returns an int value. It returns:
  • 0 if 'a is equal to 'b,
  • a positive value 'a' is greater than 'b',
  • a negative value 'b' is greater than 'a'
Below is the implementation of compareTo() method in Java: Example 1:
Output:
Comparing 20 and 20 : 0
Example 2:
Output:
Comparing 20 and 10 : 10
Example 3:
Output:
Comparing 10 and 20 : -10
Comment