×

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

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.