Javatpoint Logo
Javatpoint Logo

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.



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


Both values are equal

Example 2


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

Example 3


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

Example 4

Test it Now


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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA