• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Precedence operators versus short-circuiting

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am resurrecting an ancient thread (http://www.coderanch.com/t/192253/java-programmer-SCJP/certification/BItwise-logical-operators). From what I can tell, the issue, at least for me, wasn't addressed - although I suppose you could argue it was addressed implicitly.



The possible answers are:

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?

Thanks.

Matt

 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks.

Matt

 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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…

Thanks.

Matt


 
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh..now i got it..thankyou
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!