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 ‘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:
// Java code to demonstrate // Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 20; // byte value 'b' byte b = 20; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 20 and 20 : 0
Example 2:
// Java code to demonstrate // Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 20; // byte value 'b' byte b = 10; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 20 and 10 : 10
Example 3:
// Java code to demonstrate // Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 10; // byte value 'b' byte b = 20; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
chevron_right
filter_none
Output:
Comparing 10 and 20 : -10
Recommended Posts:
- Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
- Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
- Byte toString() method in Java with examples
- Byte compareTo() method in Java with examples
- Byte byteValue() method in Java with examples
- Byte equals() method in Java with examples
- Byte hashCode() method in Java with examples
- Byte floatValue() method in Java with examples
- Byte intValue() method in Java with examples
- Byte doubleValue() method in Java with examples
- Byte longValue() method in Java with examples
- Byte shortValue() method in Java with examples
- PrintStream write(byte[], int, int) method in Java with Examples
- Double compare() Method in Java with Examples
- Boolean compare() 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.



