×

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

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

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

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

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