DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Wellington Domiciano
Wellington Domiciano

Posted on • Originally published at wldomiciano.com

The Java's Boolean class

As of Java 9 the Boolean class constructors have become deprecated and when we want to use Boolean instances we use Boolean.TRUE or Boolean.FALSE constants.

We can get one of these constants from a primitive using the Boolean.valueOf() method.

System.out.println(Boolean.valueOf(true) == Boolean.TRUE); // true
System.out.println(Boolean.valueOf(false) == Boolean.FALSE); // true
Enter fullscreen mode Exit fullscreen mode

This method also accepts a String that, if it equals the word true, ignoring if the letters are uppercase or lowercase, the result will be equal to Boolean.TRUE, otherwise it will be Boolean.FALSE.

System.out.println(Boolean.valueOf("true") == Boolean.TRUE); // true
System.out.println(Boolean.valueOf("TRUE") == Boolean.TRUE); // true
System.out.println(Boolean.valueOf("TrUe") == Boolean.TRUE); // true
System.out.println(Boolean.valueOf("false") == Boolean.FALSE); // true
System.out.println(Boolean.valueOf("") == Boolean.FALSE); // true
System.out.println(Boolean.valueOf(null) == Boolean.FALSE); // true
Enter fullscreen mode Exit fullscreen mode

The Boolean.parseBoolean() method also accepts a String and works the same way, but returns a primitive instead of an object.

Using in expressions

We can use a Boolean directly in places where booleans expressions are expected, such as in if or while conditions.

Boolean value = Boolean.TRUE;

if (value) {
  // ...
}

while (value) {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

But be aware that as a Boolean can be null, a NullPointerException can be thrown.

Boolean value = null;

if (value) { // will throw NullPointerException
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Other cool static methods

The Boolean.logicalAnd() method is equivalent to using the && operator and returns true only if both values are true.

System.out.println(Boolean.logicalAnd(true, true));   // true
System.out.println(Boolean.logicalAnd(true, false));  // false
System.out.println(Boolean.logicalAnd(false, true));  // false
System.out.println(Boolean.logicalAnd(false, false)); // false
Enter fullscreen mode Exit fullscreen mode

The Boolean.logicalOr() method is equivalent to using the || operator and returns true if at least one of the values is true.

System.out.println(Boolean.logicalOr(true, true));   // true
System.out.println(Boolean.logicalOr(true, false));  // true
System.out.println(Boolean.logicalOr(false, true));  // true
System.out.println(Boolean.logicalOr(false, false)); // false
Enter fullscreen mode Exit fullscreen mode

The Boolean.logicalXor() methods is equivalent to using the ^ operator and returns true only if one of the values is true.

System.out.println(Boolean.logicalXor(true, true));   // false
System.out.println(Boolean.logicalXor(true, false));  // true
System.out.println(Boolean.logicalXor(false, true));  // true
System.out.println(Boolean.logicalXor(false, false)); // false
Enter fullscreen mode Exit fullscreen mode

The Boolean.getBoolean() method is used to return a System Property as a boolean primitive following to same rules as the Boolean.parseBoolean() method.

It is possible to pass to the program arbitrary System Properties using the -D option at runtime.

// Compile and run with the command below.
// java -Daaa=true -Dbbb=false Program
public class Program {
  public static void main(String... args) {
    System.out.println(Boolean.getBoolean("aaa")); // true
    System.out.println(Boolean.getBoolean("bbb")); // false
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
gabrielsantosba profile image
Gabriel Bahia.

Nossa, excelente Wellington, me clareou a mente para essa funcionalidade que para min e nova. ParabΓͺns!

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›