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 ‘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 compare() method in Java:
Example 1:
class GFG {
public static void main(String[] args)
{
byte a = 20 ;
byte b = 20 ;
int output = Byte.compare(a, b);
System.out.println( "Comparing " + a
+ " and " + b + " : "
+ output);
}
}
|
Output:
Comparing 20 and 20 : 0
Example 2:
class GFG {
public static void main(String[] args)
{
byte a = 20 ;
byte b = 10 ;
int output = Byte.compare(a, b);
System.out.println( "Comparing " + a
+ " and " + b + " : "
+ output);
}
}
|
Output:
Comparing 20 and 10 : 10
Example 3:
class GFG {
public static void main(String[] args)
{
byte a = 10 ;
byte b = 20 ;
int output = Byte.compare(a, b);
System.out.println( "Comparing " + a
+ " and " + b + " : "
+ output);
}
}
|
Output:
Comparing 10 and 20 : -10
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
05 Dec, 2018
Like Article
Save Article