×

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

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 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.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.