Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
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
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# Operator precedence

Greenhorn
Posts: 2
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
• 1
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: 23813
140
• 1

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

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.

 All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: RavenDB is an Open Source NoSQL Database thatâ€™s fully transactional (ACID) across your database https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database