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 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); } } |
Comparing 20 and 20 : 0
Example 2:
// 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); } } |
Comparing 20 and 10 : 10
Example 3:
// 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); } } |
Comparing 10 and 20 : -10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
- Date compareTo() method in Java with examples
- Double.compareTo() Method in Java with Examples
- ChronoLocalDateTime compareTo() method in Java with Examples
- UUID compareTo() Method in Java with Examples
- Path compareTo() method in Java with Examples
- Instant compareTo() method in Java with Examples
- MonthDay compareTo() method in Java with Examples
- Boolean compareTo() method in Java with examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- Year compareTo() method in Java with Examples
- LocalDateTime compareTo() method in Java with Examples
- Charset compareTo() method in Java with Examples
- OffsetTime compareTo() method in Java with examples
- LocalTime compareTo() method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- DoubleBuffer compareTo() method in Java With Examples
- ChronoLocalDate compareTo() method in Java with Examples
- Float compareTo() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

