Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean instance with the Boolean argument and returns an integer value based on the result of this method.

Syntax:

Parameters:

Here, b is the Boolean instance passed (which is compared).

Return Value:

This method returns an integer value.

  • It returns zero, if the object b represents the same Boolean value as the argument.
  • It returns positive value, if object b represents true and the argument represents false.
  • It returns a negative value, if object b represents false and the argument represents true.

Example 1

Test it Now

Output:

Both values are equal

Example 2

Output:

Enter two no.s
I no : 89
II no : 90
90 is greater

Example 3

Output:

Enter your age : 16
Sorry! you can vote after 2.0 years. 

Example 4

Test it Now

Output:

Error:(13, 19) java: boolean cannot be dereferenced

The Boolean is a primitive data type. Its object can't be used to call the Boolean class methods. If done, so it will give the above error.


Next TopicJava Boolean




Latest Courses