5.10 The Conditional Operator (?:)

The conditional operator (?:), while not a statement in itself, can be used in expressions. It evaluates a condition and contributes one of two values to the expression based on the value of the condition. The conditional operator is especially useful for handling invalid input and for outputting similar messages. The syntax of the conditional operator is shown here:

( condition ? expression1 : expression2 )

The value of an expression containing a conditional operator is determined by evaluating the condition, which is any expression that evaluates to true or false. If the condition evaluates to true, expression1 becomes the value of the expression; if the condition evaluates to false, expression2 becomes ...

Get Java Illuminated, 5th Edition 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.