Compare two boolean values

int compareTo(Boolean b)
Compares this Boolean instance with another.
boolean equals(Object obj)
Returns true if the argument is not null and is a Boolean object that represents the same boolean value as this object.

compareTo(Boolean b) returns:

ValueMeaning
zeroif this object represents the same boolean value as the argument;
a positive valueif this object represents true and the argument represents false;
a negative valueif this object represents false and the argument represents true

public class Main {
    public static void main(String[] args) {
       Boolean boolean1 = new Boolean("true");
       System.out.println(boolean1);
       
       Boolean boolean2 = new Boolean(true);
       System.out.println(boolean2.compareTo(boolean1));
    }
}

The output:


true
0
Home 
  Java Book 
    Essential Classes  

Boolean:
  1. Boolean class
  2. Get boolean value from constants in Boolean class
  3. Convert to primitive boolean value
  4. Use Boolean value constructors
  5. Compare two boolean values
  6. Get system property in boolean value
  7. Convert string to boolean
  8. Convert boolean to string