×

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result.

Syntax

public static int compare(boolean x, boolean y)

Parameters

The ‘x’ and ‘y’ represents the first and second Boolean parameters which are compared.

Return Value

This method returns an integer value which is as follows:

  • It returns an integer value zero, if x and y are equal.
  • It returns a positive integer value 1, if x is true and y is false.
  • It returns a negative integer value -1, if x is false and y is true.

Example 1:

public class JavaBooleanCompareMethodExample1 {
    public static void main(String[] args) {
        Boolean b1 = false;
        Boolean b2 = true;
        int b3 = Boolean.compare(b1,b2);
        System.out.println("Compare method returns : "+b3);
        if(b3==0){
            System.out.println("Both values are equal");
        }
        else if(b3>0){
            System.out.println("b1 value is true");
        }
        else{
            System.out.println("b2 value is true");
        }
    }
}

Output:

Compare method returns : -1
b2 value is true

Example 2:

import java.util.Scanner;
   public class JavaBooleanCompareMethodExample2 {
       public static void main(String[] args) {
           Scanner scanner=new Scanner(System.in);
           System.out.println("Enter your name and age");
           System.out.print("Name : ");String str1 =scanner.next();
           System.out.print("Age :"); int age1=scanner.nextInt();
           System.out.println();
           System.out.print("Name : ");String str2 =scanner.next();
           System.out.print("Age :"); int age2=scanner.nextInt();
           Boolean b3=(age1>age2)?true:false;
           Boolean b4=(age1<age2)?true:false;


           int val= Boolean.compare(b3,b4);
           if(val==0){
               System.out.println(str1+" and "+str2+" are equal in age");
           }
           else if(val>0){
               System.out.println(str1+" is elder than "+str2);
           }
           else{
               System.out.println(str2+" is elder than "+str1);
           }
       }
   }

Output:

Enter your name and age
Name : Sonu
Age :31
Name : Bhaskar
Age :34
Bhaskar is elder than Sonu

Example 3:

import java.util.Scanner;
   public class JavaBooleanCompareMethodExample3 {
       public static void main(String[] args) {
           Boolean b1=false;
           Boolean b2=false;
           Scanner scanner=new Scanner(System.in);
           System.out.println("Enter your name and age");
           System.out.print("Name : ");String str =scanner.next();
           System.out.print("Age :"); int age=scanner.nextInt();
           System.out.println();
           if(age>=18){
               b1=true;
           }
           int val=Boolean.compare(b1,b2);
           if (val>0){
               System.out.println(str+" you are an adult. You are eligible for voting.");
           }
           else{
               int year=18-age;
               System.out.println(str+ " you are not an adult. You will be eligible for voting after "+ year +" years.");
           }
       }
   }

Output

Enter your name and age
Name : Reema
Age :16


Reema you are not an adult. You will be eligible for voting after 2 years.

Related Topics

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Java Boolean parseBoolean() Method

The parseBoolean() method of Boolean class returns a Boolean value for the specified String argument. It returns true if and only if string’s value is equal to “true”, else it...

1 minute read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.