Originally posted by Sarma Lolla:
So what is the final conclusion form this?
Of course that question always produces the same response: the Java Language Specification always provides the final conclusion. In this case, please see
Section 15.7, Evaluation Order. For more information on the conditional "and" operator, &&, please see
Section 15.23.
Remember that Java always parses expressions from left to right and evaluates each operand of the expression from left to right. Java respects operator precedence and parenthesis so many of the operands will be temporarily saved on the operand stack until they are needed.
In this question, the left hand operand, (a = true), of the conditional "or" operator, ||, is evaluated and the result is true. According to the JLS, evaluation of a conditional "or" expression ends if the left hand operand is found to be true. The right hand operand is never even evaluated. Similarly, the rest of the expression is never evaluated.
Suppose that the left hand operand of the conditional "or" expression was false instead of true. If that were the case, then the right hand operand would be evaluated. In this case, the right hand operand of the conditional "or" operator, ||, is actually an expression with higher precedence, (b = true) && (c = true). The entire conditional "and" expression, (b = true) && (c = true), would be evaluated and the result would serve as the right hand operand of the earlier conditional "or" expression.
Although Java does respect operator precedence and parenthesis Java still parses expressions from left to right. The operand stack is used to save the intermediate results until they are needed.
[ February 05, 2003: Message edited by: Dan Chisholm ]