• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compound Assignment Operator  RSS feed

 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

This is a fairly simple question... But I just cant seem to understand the order of precedence here.



From what I have read compound operators have the lowest order of precedence...
But the above piece of code makes the assignment k = 1 + (k = 4) * (k + 2) before evaluating the rest of the statement first.
It then evaluates (k = 4) and proceeds with the remained of the statement 1 + 4 * (4 + 6)....
I dont understand why the first k is assigned 1 but the remaining ks 4.
Should they not all be 1 or 4 (I would have thought 4, since += has the lost order of precedence so is evaluated last)??




 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you say that k =1 is evaluated first?
What do you have as the result?
 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result is 25.
 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k = 1
k += (k = 4) * (k + 2)
k = 1 + (k = 4) * (k + 2)
k = 1 + 4 * 6
k = 1 + 24
k = 25
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Obrien wrote:The result is 25.


and


=>


=>


=>

=> k+1 is done last
 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is k not assigned to 4 though in the middle of that statement (k = 4)
if k += 24 is the last part to be executed, is k not 4 at that stage....
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Obrien wrote:Is k not assigned to 4 though in the middle of that statement (k = 4)
if k += 24 is the last part to be executed, is k not 4 at that stage....


No the left-hand side of a compound assignment is saved before the right-hand side is evaluated
This is explained in the JLS at the end of 15.26.2 where your example code is cited.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find that code? It just goes to show what sot of confusing code there is available.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is in the JLS. As EA said, along with an explanation.
 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

I read through the JLS and obviously makes perfect sense now. It seemed to be fairly well hiden in there, I couldnt find an explaination before hand.
I found the code just going through a list of exam style questions for the Oracle Certified Associate, Java SE 7 Programmer exam.
It gave the answer alright but didnt fully explain how to reach the answer! Just said to follow the order of precedence for operators.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the precedence order in that instance is (=), (+), *, +=
The exact details are in the JLS. Unlike some JLS code examples, that is there as one to avoid!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!