What does


5.4 Logical operators



Logical operators are used to boolean Linking values ​​together. In contrast to the relational operators, which first produce truth values ​​through comparisons, logical operators are used for further processing of truth values.

Java provides the basic operations AND, OR and NOT available and also offers the option of influencing the evaluation behavior of the operands. Unlike most other programming languages, Java provides the AND and OR operations in two different versions, namely with Short circuit evaluation or without.

In the short-circuit evaluation of a logical expression, a partial expression further to the right is only evaluated if it is still relevant for the result of the overall expression. If is already wrong in the expression, it will inevitably always be wrong, regardless of the result of. In this case, the short-circuit evaluation will no longer be evaluated. The same applies to the use of the OR operator.

The one also available in Java EXCLUSIVE-OR operator must of course always be evaluated in the long variant. Table 5.3 gives an overview of the logical operators.

operatordescriptionimportance
!Logical NOT! a returns false if a is true and true if a is false.
&&AND with short-circuit evaluationa && b returns true if both a and b are true. If a is already false, false is returned and b is no longer evaluated.
||OR with short circuit evaluationa || b returns true if at least one of the two expressions a or b is true. If a is already true, true is returned and b is no longer evaluated.
&AND without short-circuit evaluationa & b returns true if both a and b are true. Both sub-expressions are evaluated.
|OR without short circuit evaluationa | b returns true if at least one of the two expressions a or b is true. Both sub-expressions are evaluated.
^Exclusive-ORa ^ b returns true if both expressions have a different truth value.

Table 5.3: Logical operators