Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Operator precedence  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm preparing for OCA exam and I have a question about operator order of precedence.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
I'm especially interested in postfix and unary order.
Its sad that postfix have higher precedence than unary operators and they should be evaluated before.

Given some sample code:

i would say that its executed as:

but when i run the program i get:

i even get to decomplie my example code and got:

which says clearly that pre-decrements (line 2 and 7) were executed before post increment (line 17).

Is this an bug in documentation or I misunderstand the idea of order of precedence in this case?
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The output is 7.0 indeed. Why? Let dissect it.

x=3
y = --x + --x * 5.0 / x++

the first --x becomes 2 (pre-decrement is decrement then assign)
the second --x becomes 2 also (not 1 because the first and second --x are done at the same time)
* 5.0
/ x++ now the x here is 2 not 3 (post-increment is assign then increment)

Therefore y = 2 + ((2 * 5.0) / 2) => 2 + (10.0 / 2) => 2 + 5.0 => 7.0

Put in the parenthesis to emphasize the operator precedence
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Ozga wrote:
Its sad that postfix have higher precedence than unary operators and they should be evaluated before.


... which says clearly that pre-decrements (line 2 and 7) were executed before post increment (line 17).

Is this an bug in documentation or I misunderstand the idea of order of precedence in this case?




Basically, you are confusing precedence with order of evaluation... For the most part, expressions are evaluated from left to right regardless of the precedence. See...

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry
 
Robert Ozga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
Basically, you are confusing precedence with order of evaluation... For the most part, expressions are evaluated from left to right regardless of the precedence. See...

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7



That's what I was missing.

After some research I found this quote at Operator Precedence

Precedence order. When two operators share an operand the operator with the higher precedence goes first.



Thank you Henry.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!