java.lang.Byte compare()
Description
Important Notes:
- The method
compare(byte x, byte y)
will yield the same result asByte.valueOf(x).compareTo(Byte.valueOf(y))
Method Syntax
public static int compare(byte x, byte y)
Method Argument
Data Type | Parameter | Description |
---|---|---|
byte | x | the first byte to compare |
byte | y | the second byte to compare |
Method Returns
The compare() method of Byte class returns the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
Compatibility
Requires Java 1.7 and up
Discussion
Java Byte compare(byte x, byte y) Example
Below is a simple java example on the usage of compare() method of Byte class.
package com.javatutorialhq.java.examples; /* * This example source code demonstrates the use of * compare(byte x, byte y) method of Byte class. */ public class ByteCompareExample { public static void main(String[] args) { // initialize 3 byte primitive byte firstValue = 12; byte secondValue = 12; byte thirdValue = 15; // compare the first byte to the second int compareOneTwo = Byte.compare(firstValue, secondValue); // compare the first byte to the third int compareOneThree = Byte.compare(firstValue, thirdValue); // display the comparison result of first // and second value if (compareOneTwo == 0) { System.out.println("First and second value are equal"); } else if (compareOneTwo > 0) { System.out.println("First value is greater than second value"); } else { System.out.println("First value is less than second value"); } // display the comparison result of first // and second value if (compareOneThree == 0) { System.out.println("First and third value are equal"); } else if (compareOneTwo > 0) { System.out.println("First value is greater than third value"); } else { System.out.println("First value is less than third value"); } } }
Basically on the above example, we have declared three Byte values. We then use the compare method to do comparison of these three values. Based on the result of this method, we have devised a logic to display specific messages.
Sample Output
Below is the sample output when you run the above example.