java.lang.Boolean compare()
Description
Important Notes:
- The method
compare(boolean x, boolean y)
will yield the same result asBoolean.valueOf(x).compareTo(Boolean.valueOf(y))
Method Syntax
public static int compare(boolean x, boolean y)
Method Argument
Data Type | Parameter | Description |
---|---|---|
boolean | x | the first boolean to compare |
boolean | y | the second boolean to compare |
Method Returns
The compare() method of boolean 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
Discussion
Java Boolean compare(boolean x, boolean y) Example
Below is a simple java example on the usage of compare() method of Boolean class.
package com.javatutorialhq.java.examples; /* * This example source code demonstrates the use of * compare(boolean x, boolean y) method of Boolean class. */ public class BooleanCompareExample { public static void main(String[] args) { // initialize 3 boolean primitive boolean firstValue = true; boolean secondValue = false; boolean thirdValue = true; // compare the first boolean to the second int compareOneTwo = Boolean.compare(firstValue, secondValue); // compare the first boolean to the third int compareOneThree = Boolean.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 just ask for two numbers on the console and then we use the scanner object to get the float inputs. We then determine which of the two numbers were higher using the compare method.
Sample Output
Below is the sample output when you run the above example.