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
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# 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: 23830
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.

 Ever since I found this suit I've felt strange new needs. And a tiny ad: Create Edit Print & Convert PDF Using Free API with Java https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire