×

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

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.