Hi, The || and && operators don't evaluate the right expression if the left one is evaluated to true (|| operator) or false (&& operator). In these cases, the result value is straight forward (it isn't necessary to evaluate the right expression)
Joined: Nov 22, 2008
Sorry, Forget the answer I now see your point.
Joined: Nov 22, 2008
I think the different operands in a expressi�n are always evaluated in the order their appear in the expression. Other point is when the operation is performed: that depends on operator precedence. Example 1: a + b * c a evaluated b evaluated c evaluated * performed (with c and b) + performed (with the former result and a) Example 2: a * b + c a evaluated b evaluated * performed (with a and b) c evaluated + performed (with the former result and c) Example 3: a || b && c a evaluated: if false doesn't eval. the right expr
Java respects operator precedence and parenthesis, but it parses the expression from left to right. Once the JVM realizes that the left operand of a conditional "or" operator has evaluated to true the JVM knows that it is required NOT to evaluate the rest of the expression. Remember, the conditional "and" operator, for example, is often used to prevent the evaluation of the right hand operand if the left hand operand evaluates to false. The left hand operand expression of a conditional "and" operator is often used to verify that a reference is not null. The right hand operand then uses the reference to invoke a method on the object.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Hello Jose Francisco, I think it can be summarized like this: In the absense of parentheses and &&, || operators when considering if the operation of a given operator is performed or not, we must note the precedence of the operators to its left and the precedence of those to its right. An operation is performed before the operators to its left if they has fewer precedence and before the operators to its right if the precedence of those is equal or fewer.
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 ]