Boolean.compare() in Java

Boolean.compare(): This method is available in the java.lang.Boolean class of Java.

Syntax:

int java.lang.Boolean.compare(boolean x, boolean y)

This method takes two arguments of type boolean as its parameters. This method compares two boolean values.

Parameters: Two parameters are required for this method.

x: the first boolean to compare.

y: the second boolean to compare.

Returns: 

1. The value 0 if x == y.

2. A value less than 0 if !x && y.

3. A value greater than 0 if x && !y.

For Example:

Boolean.compare(true,false) = > It returns 1.

Approach

Java

public class BooleanCompare {
    public static void main(String[] args) {

        boolean x = true, y = false;
        System.out.println(Boolean.compare(x, y));

    }
}

Output:

1

No comments:

Post a Comment