Boolean Expressions

A Boolean expression is a Java expression that, when evaluated, returns a Boolean value: true or false. Boolean expressions are used in conditional statements, such as if, while, and switch.

The most common Boolean expressions compare the value of a variable with the value of some other variable, a constant, or perhaps a simple arithmetic expression. This comparison uses one of the following relational operators:

Operator

Description

==

Returns true if the expression on the left evaluates to the same value as the expression on the right.

!=

Returns true if the expression on the left does not evaluate to the same value as the expression on the right.

<

Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right.

<=

Returns true if the expression on the left evaluates to a value that is less than or equal to the expression on the right.

>

Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right.

>=

Returns true if the expression on the left evaluates to a value that is greater than or equal to the expression on the right.

CrossRef.eps For more information, see Variables and Constants.

A basic Boolean expression has this form:

expression relational-operator expression

Java evaluates a Boolean expression ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.