boolean

The primitive type "boolean" contains one of the values "true" or "false". It is usually used with commands directing the program flow, such as "if", "while", and "do", or to express whether an object has some trait (e.g. whether a button is enabled, or visible).

ab a & b, a && b a | b, a || b a ^ b
falsefalse false false false
falsetrue false true true
truefalse false true true
truetrue true true false

Operators "&" and "&&" both return the same result; as do operators "|" and "||". The only difference is whether values on both sides are evaluated always ("&", "|"), or only when needed ("&&", "||").

For example, when operator "&&" evaluated the left side of the expression as "false", it will not evaluate the right side, because either way the result would be "false". Similarly, when operator "||" evaluated the left side as "true", it will not evaluate the right side, because either way the result would be "true". This can make a difference when the right side of the expression is a function call; the function will be called only when necessary to determine the outcome of the expression.

Here is an example of calling functions which, as a side effect, write a message on standard output:

public class BooleanOperators {

  static boolean t(String message) {  // always returns true
    System.out.println(message);
    return true;
  }

  static boolean f(String message) {  // always returns false
    System.out.println(message);
    return false;
  }

  public static void main(String[] args) {
    System.out.println(f("A11") & f("A12"));
    System.out.println(f("A21") & t("A22"));
    System.out.println(t("A31") & f("A32"));
    System.out.println(t("A41") & t("A42"));
    System.out.println(f("B11") && f("B12"));  // only calls B11
    System.out.println(f("B21") && t("B22"));  // only calls B21
    System.out.println(t("B31") && f("B32"));  
    System.out.println(t("B41") && t("B42"));
    System.out.println(f("C11") | f("C12"));
    System.out.println(f("C21") | t("C22"));
    System.out.println(t("C31") | f("C32"));
    System.out.println(t("C41") | t("C42"));
    System.out.println(f("D11") || f("D12"));
    System.out.println(f("D21") || t("D22"));
    System.out.println(t("D31") || f("D32"));  // only calls D31
    System.out.println(t("D41") || t("D42"));  // only calls D41
  }

}

Class java.lang.Boolean

The class "Boolean" wraps the value of one "boolean". Because it is an object, it behaves a little differently from the primitive type. First, it can technically contain three possible values: a TRUE value, a FALSE value, or a null reference. Second, there could exist multiple TRUE objects and multiple FALSE objects, therefore it is not safe to compare them using the "==" operator. Here are the best practices for using Boolean objects:

Unless you seriously need that, avoid putting "null" values into "Boolean" variables.

To create a new TRUE or FALSE value, use the predefined constants "Boolean.TRUE" and "Boolean.FALSE". (Don't use a constructor.)

Boolean yes = Boolean.TRUE;
Boolean no = Boolean.FALSE;

To convert a "boolean" or a "String" value to a "Boolean" value, use the "Boolean.valueOf" method. (Don't use a constructor.)

Boolean yes = Boolean.valueOf("true");
boolean b = ...;
Boolean objB = Boolean.valueOf(b);

viliam@bur.sk