×

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result of this method.

Syntax

public int compareTo(Boolean b)

Parameters

The parameter ‘b’ represents the Boolean instance which is compared.

Return Value

This method returns an integer value based on the result.

  • It returns zero, if the parameter b is equal to Boolean instance.
  • It returns positive 1, if object b represents true and the argument represents false.
  • It returns a negative 1, if object b represent false and the argument represent true.

Example 1

public class JavaBooleanCompareToMethodExample1 {
    public static void main(String[] args) {
        Boolean aBoolean = new Boolean(false);
        Boolean bBoolean = true;
        //compare b1 with b2 and return an integer value
        int b3 = bBoolean.compareTo(aBoolean);
        System.out.println("compare To will return: "+b3);
        if(b3==0){
            System.out.println("Both values are equal");
        }
        else if(b3>0){
            System.out.println("aBoolean value is true");
        }
        else{
            System.out.println("bBoolean value is true");
        }
    }
}

Output

compare To will return: 1
aBoolean value is true

Example 2

public class JavaBooleanCompareToMethodExample2 {
    public static void main(String[] args) {
        Boolean b1=true;
        System.out.print("Even number between 1-10 : ");
        for (int i=1;i<=10;i++){
            Boolean b2=true;
          if(i%2==0){
              b2=false;
          }
          // compare b1 ana b2 and return an integer value
          int val=b1.compareTo(b2);
          if (val>0){
              System.out.print(i+" ");
          }
      }
    }
}

Output

Even number between 1-10 : 2 4 6 8 10

Example 3

public class JavaBooleanCompareToMethodExample3 {
    public static void main(String[] args) {
        Boolean b1=true;
        boolean b2=false;
        //boolean b2 is an primitive data type its object cannot be used to call Boolean class methods
        int value =b2.compareTo(b1);
        System.out.println("Compare To method will return : "+value);  
      
    }
}

Output

Error:(8, 22) java: boolean cannot be dereferenced

Related Topics

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

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

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

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

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.