Boolean operators help to build more complex expressions by combining two or more conditional expressions into a larger one.

Each of the operands of such an expression must evaluate to a boolean value:

Conditional AND (&&)

This operator combines two expressions that evaluates to true, and if both are true – the whole expression evaluates to true. For example:

1 2 3 4 5 6 7 8 |
int i = 5; if (i < 10 && 2 < i) { //... } |

Conditional OR (||)

This operator combines two expressions and at least one of them have to evaluate to true, in that case – the whole expression evaluates to true. For example:

1 2 3 4 5 6 7 8 9 |
int i = 5; if (i < 10 || 8 < i) { //Only the first expression i < 10 evaluates to true but since only one of them //have to be true, the if-block is entered at execution time. } |

Boolean NOT (!)

This operator changes the boolean value that an expression is evaluated to.

If the expression evaluates to true, then the operator changes it to false and vice versa.

For example:

1 2 3 4 5 6 7 8 9 10 |
int i = 5; if (!(i < 10 && 8 < i)) { //Without the operator the expression evaluates to false since 8 is not less than 5 (i), //but by putting parentheses around it and adding the ! in front of the expression, //it instead evaluates to true. } |

It’s the same thing as writing:

1 2 3 4 5 6 7 8 |
int i = 5; if ((i < 10 && 8 < i) == false) { //It does evaluate to false, so the condition is true and the if-block is entered at execution time. } |

Search for more Java info on this site here: