×

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 Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Java Math random() Method

The random() method of Math class returns a double value with a positive sign, less than 1 and greater than or equal to 0.0. This method is properly synchronized with...

1 minute read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.