Tuesday, May 24, 2011

Conditional operators in java

The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left: (a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)

int x = 10, y = 12, z = 0;
z = x > y ? x : y;
//As x>y is false, z=y

No comments:

Post a Comment

Chitika