×

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

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.