Thursday, 28 May 2015

Java Ternary Conditional Operator (? :)

The ternary conditional operator has the following syntax:

<condition> ? <expr1> : <expr2>

If condition (boolean expression) is true then <expr1> is evaluated otherwise <expr2> is evaluated.

For example, the following code segment, will store the maximum of x and y into variable max:

max = (x > y) ? x : y;

Here it is assumed that the variables x, y and max are of numeric type. The type of the variable max should be higher or same as the higher type among types of x and y.

No comments:

Post a Comment