×

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

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

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

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java String Reader

StreamReaderClass: This class is present in the java.io package. It is a character stream in which string act as a source.This method provides to read characters from a string. Character Stream: This class...

3 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.