Byte compareTo() method in Java with examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java code to demonstrate // Byte compareTo() method class GFG { public static void main(String[] args) { // creating a Byte object Byte a = new Byte("20"); // creating a Byte object Byte b = new Byte("20"); // compareTo method in Byte class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 20 and 20 : 0 Example 2: Java // Java code to demonstrate // Byte compareTo() method class GFG { public static void main(String[] args) { // creating a Byte object Byte a = new Byte("20"); // creating a Byte object Byte b = new Byte("10"); // compareTo method in Byte class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 20 and 10 : 10 Example 3: Java // Java code to demonstrate // Byte compareTo() method class GFG { public static void main(String[] args) { // creating a Byte object Byte a = new Byte("10"); // creating a Byte object Byte b = new Byte("20"); // compareTo method in Byte class int output = a.compareTo(b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 10 and 20 : -10 Comment More infoAdvertise with us Next Article Calendar compareTo() Method in Java with Examples S ShivamKD Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-Byte java-lang-reflect-package +2 More Practice Tags : JavaMisc Similar Reads Byte compare() method in Java with examples The compare() method of Byte class is a built in method in Java which is used to compare two byte values. Syntax Byte.compare(byte a, byte b) Parameters: It takes two byte value 'a' and 'b' as input in the parameters which are to be compared. Return Value: It returns an int value. It returns: 0 if ' 2 min read Boolean compareTo() method in Java with examples The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return 2 min read Calendar compareTo() Method in Java with Examples The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type an 2 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Double.compareTo() Method in Java with Examples The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object, an 3 min read Like