• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Operator Precedence  RSS feed

 
Elly Vassallo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have encountered a problem with operator precedence in Java. I have been trying to figure it out with no avail! :S

When I type the following code:

I know maybe I should avoid expressions which have side-effects. But I still cannot understand how this works.

Shouldn't the two calculations yield the same result? The postfix operator has a higher precedence than minus and plus (that's what it says here: oracle). So shouldn't it have been evaluated first in both expressions?

Thanks in advance for sharing your knowledge.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also consider the rule which says that the operands of an operator are evaluated from left to right.

In your first example that means that "k++" and "k-1" are executed in that order, then added together. In your second example it means that "m-1" and "m++" are executed in that order, then added together.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial you linked to actually explains this a little further ahead


The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.


At the bottom of the page here: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you so that you can see the difference.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some of the links at the end of this old post may help you.

Lots of people get confused about ++i and i++.
 
Elly Vassallo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think some of you have got me misunderstood here. I have no problem understanding the difference between postfix and prefix operators.

What was unclear to me was operator precedence. Since ++ has a higher precedence than the minus operator I thought that when in an expression you have the ++ and minus operator, the ++ always gets worked out first. Which is not the case, since:

if for example k is 2, and you have:
1) k++ + (k-1)
2) (k-1) + k++

The first expression results in a 4 (making it clear that k++ was worked out before the k-1), and the second expression results in a 3 (making it clear that k-1 was worked out before k++).

So as you see my problem is not understanding what k++ does, but the operator precedence.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Operator precedence is important when determining which operator should be performed first on a specific operand. However, operations aren't evaluated by precedence, but always from left to right.

This matters in the case of assignment operators and the increment/decrement operators, and when performing a method call. Be wary of methods with side-effects.

Then there's also a third thing to keep in mind, namely operator associativity. When two operators have the same operand as their input, and they have the same precedence, the associativity determines with which of the two operators the operand is associated.

Let's look at your example:

k++ + (k-1)
2++ + (k-1)
2 + (k-1)
2 + (3-1)
2 + (2)
4

(m-1) + m++
(2-1) + m++
(1) + m++
1 + 2++
1 + 2
3
 
Elly Vassallo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just what I needed..understood

Thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!