# Complex boolean Expressions

Complex boolean Expressions

# Complex `boolean` Expressions

Java has operators for combining boolean values with AND and OR logic, and for negating a value (a NOT operation). Note that:

• There are also bitwise operators for AND, OR, and bitwise inversion (changing all 1 bits to 0, and vice versa).
• Since a boolean is stored as one bit, the bitwise AND and OR operators will give the same logical result, but will be applied differently (see below).
• For some reason, the bitwise NOT cannot be applied to a `boolean` value.
Logical Bitwise
`&&` logical AND `&` bitwise AND
`||` logical OR `|` bitwise OR
`!` logical NOT `~` bitwise NOT (inversion)

Examples:

Code Effect
Testing if a value falls within a range

```( ( a >= 0 ) && ( a <= 10 ) )```

is `true` if `a` is falls between 0 and 10; it must satisfy both conditions

Testing if a value falls outside a range

```( ( a < 0 ) || ( a > 10 ) )```

is `true` if `a` falls outside the range 0 to 10; it may be either below or above that range

```( !( ( a >= 0 ) && ( a <= 10) ) )```

inverts the test for `a` between 0 and 10; so `a` must be outside the range instead of inside it

The `&&` and `||` operations are called short-circuiting, because if the first condition determines the final outcome, the second condition is not evaluated.

To force both conditions to be evaluated, use `&` and `|` for AND and OR, respectively.

Example: to test if a reference is `null` before calling a `boolean` valued method on it.

```String s = request.getParameter("shipovernite");
if (s != null && s.equalsIgnoreCase("YES")) { . . . }```

`equalsIgnoreCase` will be called only if the reference is not `null`.