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
Java
Java
Java
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'
// 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:
Example 2:
Comparing 20 and 20 : 0
// 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:
Example 3:
Comparing 20 and 10 : 10
// 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