1. 1st digit printed is 1
2. 1st digit printed is 2
3. 1st digit printed is 3
4. 2nd digit printed is 1
5. 2nd digit printed is 2
6. 2nd digit printed is 3
Why don't the precedence rules affect the answer?
That is, shouldn't what's in the Parentheses be resolved first, followed by resolving the Less Than, <, and lastly resolving the Or, ||?
It seems to me the jls has turned the set of precedence rules on its head. That is, the jls seems to say "here are the precedence rules, where || has less priority than parentheses and less priority than < except sometimes those precedences don't matter and || overrides everything else with the highest precedence". Is this a nuance or am I missing a rule?
What wasn't said in that old thread (not explicitly, anyway) was that evaluation of expressions proceeds from left to right. In other words the LHS of an expression is always evaluated before the RHS -- and as you know, the RHS may not be evaluated at all.
posted 7 years ago
Thank you Paul.
If expressions are evaluated left to right, what then is the use of a precedence hierarchy?
Question 1: What is it about this particular expression that tells the compiler (or jvm) to evaluate the expression left to right?
Question 2: If this particular expression is not subject to precedence rules, how would an expression look differently which would be subject to precedence rules?
matt love wrote:If expressions are evaluated left to right, what then is the use of a precedence hierarchy?
The precedence hierarchy describes which operators get applied to those expressions in which order. Let's take two examples:
In the first example, A is evaluated first, then B * C (because * has higher precedence than + does). And when evaluating B * C, first B is evaluated and then C.
In the second example, A * B is evaluated first (because of precedence, again) and then C is evaluated. And when evaluating A * B, first A is evaluated and then B.
You'll notice that in both examples, the net result was that first A is evaluated, then B, then C. However the three expressions are combined differently because of the operator precedence rules.
posted 7 years ago
Thanks again Paul.
Your first example, A + B * C, is perfect. The * operator is evaluated before the + operator, but after the variable A is evaluated.
What of A || B * C?
Noted, a more realistic expression would be (A || (0 < (B * C))).
Like your example, the * trumps the ||.
And then, if we were to substitute (0 < (B * C)) with (0 < (someInt++)), we’re back to the beginning in that from my way of looking at it (contrary as it is to your way and the compiler’s way) is that someInt++ is executed before short-circuiting occurs.
I appreciate the time you’ve put into this subject and cordially invite you to take a pass rather than extending the…
Let's take the expression A || 0 < B * C. Arithmetic has a higher precedence than relational operators, which in turn have higher precedence than logical operators.
However, the expression is always evaluated from left to right. In this example, if A was true, the expression would evaluate to: true || 0 < B * C, which in turn evaluates to true, because of short-circuit evaluation.
If A is false, the result would be: false || 0 < B * C, which evaluates:
So as you can see, if we replace B and C with some sort of increment expression, it only gets evaluated if A is false.
Stephan nad Paul, I would appreciate if you have a look at the code below and answer my query. What if we write a code like this-
I know that expression (true || (0 >= ++i)) will be solved left to right and true will be assigned to t without execution of (0 >= ++i)). But the expression (true || (0 >= ++i)) is enclosed into parenthesis and priority of parenthesis is higher than assignment. So, shouldn't the whole expression inside parenthesis be executed first before assignment. why this is not executed like this-
Please help me to be clear about this. Thanks in advance.
Astha - OCPJP 6 (90%)
Stephan van Hulst
posted 7 years ago
No. Precedence has to do with which operands bind to which operators. It has nothing to do with order of execution. Order of execution (evaluation) is *always* left-to-right in Java.
Parentheses have the highest precedence. This means that operands within the parentheses bind to operators within the same parentheses. Not that they evaluate first.