Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about post/pre unary operator precedence (Java OCA 8 Programmer I Study Guide, Sybex)

 
Anton Medvedev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. Sorry if this have already discussed before.
There is a thing I cannot understand. And if the problem exists - this doesn't look like a book problem

Table 2.1 on page 52 says that Post-unary operators have higher priority than Pre-unary. So I think they should be processed first.

But below on page 59 there is an example :



and it processed as post and pre-unary operators had same priority. (first "++x" replaced with 4, but we have "x--")

As I understand this example gives same results no matter how you treat the priority.
Documentation sets the same priority order https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
but this example

returns 1 and 5 proofing JVM treats them as same priority

Two questions:
1) Am I right saying all these things?
2) If I'm right how should I answer that kind of questions on the exam?
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of a postfix increment/decrement expression is the value of the variable before the new value is stored. i.e. it isn't until the next expression that uses the variable that the new value is used.

The value of the a prefix increment/decrement expression is the value of the variable after the new value is stored. i.e. the new value of the variable is used on the line of code that the expression is on.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anton Medvedev,

First of all, a warm welcome to CodeRanch!

Anton Medvedev wrote:Two questions:
1) Am I right saying all these things?
2) If I'm right how should I answer that kind of questions on the exam?

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.

Hope it helps!
Kind regards,
Roel
 
Anton Medvedev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel, I've read all these treads.
All I can see from there is they are corrent if post-unary and pre-unary operators have same precedence.
And they look like they have in JVM implementation. Thats why they process from left to right.

the only problem here - the table of precedence https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
according to it "expr++ expr--" are higher than "++expr --expr"
like "*" higher than "+"

looks like implementation doesn't conform the specification.
At all test question I'll process them from left to right. Hope that will help.

Thank you again
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton Medvedev wrote:looks like implementation doesn't conform the specification.
At all test question I'll process them from left to right. Hope that will help.

Please note that when you have to evaluate an expression, then several factors are involved. So the order of evaluation is not only determined by the operator predence. This thread has an excellent explanation about many of these factors with a bunch of code snippets (often using the short-circuit operators). Definitely a must read as well!
 
Anton Medvedev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel. I'll sure read

 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having browsed through the threads referenced here, none of them explain why the postfix increment operator has higher precedence than the prefix increment operator. Since precedence really comes into play when operators compete over the same operand, the fact that the postfix increment operator has higher precedence than the prefix increment operator has no effect because these operators never compete over the same operand. In this thread, Henry Wong seems to back up my point.

Both the pre increment and post increment operator can only be applied to a variable, and not to an expression, so you can't apply them both at the same time, and hence, there is no meaning in saying one has precedence over the other.

Not all things in the precedence table are comparable, sometimes they are listed where they are for other operators in the table.

After operands are evaluated from left to right, the fact that the postfix increment operator has higher precedence than the prefix increment operator has no effect. So the question is what does it mean when the JLS says that the postfix increment operator has higher precedence than the prefix increment operator? What effect does this higher precedence have?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:Having browsed through the threads referenced here, none of them explain why the postfix increment operator has higher precedence than the prefix increment operator. Since precedence really comes into play when operators compete over the same operand, the fact that the postfix increment operator has higher precedence than the prefix increment operator has no effect because these operators never compete over the same operand. In this thread, Henry Wong seems to back up my point.

True! If you try to write something like this, you'll get a compiler errorAnd even adding parantheses won't be of any help, still a compiler errorPlease note: these statements will compile without any errorsYou are not using the prefix increment/decrement operator in these statements, but the unary plus/minus operator (which is already nicely explained in this topic).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic