This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Operator precedence

 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

As I said in another topic, I'm studying for OCA Exam, with the book OCA Java SE 8 Programmer I Study Guide.

On page 59 there is an example that I guess it is wrong.

The code goes like this:


And right after, there is an explanation about it, saying that the x begins to be incremented in the first statement (++x), and shows the following sequence:



But I believe the sequence should be this one:


So the result should be x = 2 and y = 7

In both sequences, the result is the same. But I believe the sequence presented in the book is wrong, not following the precedence level. Am I right? Or am I missing something?

Thanks guys, I'm waiting for the clarifications.

Have a good night.
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn, I forgot to change some values.

As I can't edit the message, I will post here what I believe is the right sequence


Sorry for the mistake.
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expressions are always evaluated from left to right, operator precendence only determines what belongs to an operand (i.e. in the case of 3 + 2 * 7 the operator to start evaluating with would be the '+' with operands (3) and (2 * 7) -> (3) is evaluated first, then (2 * 7)).
For example change x to 0. Following your described evalution order, the (x--) should be evaluated first which would lead to an arithmetic exception (5 / 0), but as the expression is evaluated from left to right, x is incremented first -> 1 * 5 / 1 + -1 = 4
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tobias,

Thanks for the answer.

I've tested the example that you mentioned, with x being 0, and you were right, it does work.

But I got a little confused right now. My understanding was like demonstrated in Oracle's tutorial: "Operators with higher precedence are evaluated before operators with relatively lower precedence".
Now, I didn't understand the sequence of post and pre unary operators in the example of this topic.
Could you please give me an example of the precedence of these unary operators being applied?

Thank you very much.
 
author
Posts: 23873
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:My understanding was like demonstrated in Oracle's tutorial: "Operators with higher precedence are evaluated before operators with relatively lower precedence".



Well, I guess there are two ways to respond here.

One. The Oracle tutorial is wrong...

Two. Perhaps Oracle is referring to when evaluation completes. Let me explain... let's start with this.... 2 + 3 * 4.

Evaluation is parsing the components. For binary operation, such as the plus operator, it is handling the LHS (left hand side), then the operator, and then the RHS (which of course, can be a complex expression). Only then, can the operation take place, and the result calculated.

Evaluation always go from left to right, meaning that the parsing goes from left to right. However, actually, completion of evaluations can't complete until all parts are evaluated first. This means that operations in the RHS can complete first. In this example, the "3 * 4" expression completes before the plus operator completes, even though the plus evaluation started first.

Henry
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Henry

Thanks for the explanation

So, in the following example, the evaluation first parses the elements, starting with the ++x, going to the right until it decides the value of each operand (in case of pre/post unary, the return of it), until it completes the expression evaluation, remaining just the operands and arithmetic operators?



Besides the tutorial, I got the same understanding from the books that I'm studying for the exam

Sorry for the insistence, but these operators are really bothering me :S
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand it now

I linked Tobias's and Henry's answer, and I think I got it

With the following code


The evaluation goes from left to right. Thus, the first expression is ++x * 5. ++x is one operand of the *, and 5 is the other. This expression is calculated first (4 * 5), resulting in 20, and the x value being 4. Then, the next expression is 20 / x--, evaluated as 20 / 4, resulting in 5, with x value being 3. Finally, the last operand is --x, which means that the final expression is 5 + 2, resulting in y = 7, and the final x value is 2.
So, I can't analyze just the operand, I need to analyze each operation (operands and its operator). So in this case I can't analyze the precedence of the post/pre unary operators separately, because they are and operand of another expression.

It is something like that?
 
Henry Wong
author
Posts: 23873
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:
So, I can't analyze just the operand, I need to analyze each operation (operands and its operator). So in this case I can't analyze the precedence of the post/pre unary operators separately, because they are and operand of another expression.



To be honest, I am not sure what you are trying to say about precedence. It is what it is -- and it is not effected by the expression that it is in. For example, take your expression...

Post increment and decrement has the highest precedence, so this expression is equivalent to this...

Pre increment and decrement has the next highest precedence, so this expression is equivalent to this...

Multiplication and Division has the next highest precedence, so this expression is equivalent to ... well, not yet... You also have to take into account that these operators have left to right associativity, so, this expression is equivalent to this...

Addition has the next highest precedence, so this expression is equivalent to this...

And finally, the assignment has the lowest precedence, so the completion of the assignment is last, even though it was the first to start evaluation... since evaluation is left to right.

Notice that I simply applied the precedence on the expression, in the order of the table. The precedence table did not change based on the expression.

Henry
 
João Victor Gomes
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see.

Now I'm back to the square one, haha

I guess I was in a better path, and then I just started to confuse my head.

I think the problem is that I was taking the "precedence level" too literally.
I was thinking that the post-unary - because of its higher precedence - would be evaluated and calculated before everything else.
But actually, before everything else, the precedence of each operand is defined, and then, the expression is calculated from left to right, respecting the precedence defined before.

Am I getting better?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:In both sequences, the result is the same. But I believe the sequence presented in the book is wrong, not following the precedence level. Am I right? Or am I missing something?


That's really a very common question (confusion) on this forum! You should definitely read this topic and this one as both topics provide excellent explanations (with illustrative code snippets) about the same code snippet. And if you want to learn more about the order of evaluation of (post-)increment (& decrement) operators, this topic is also definitely worth reading. And if you want to learn more about evaluating boolean expressions with short-circuit operators this topic and this one are must-read topics.

Hope it helps!
Kind regards,
Roel
 
Lookout! Runaway whale! Hide behind this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic