This week's book giveaway is in the OCP forum.We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP 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:
Sheriffs:
Saloon Keepers:
Bartenders:

# Compound Assignment Operator

Greenhorn
Posts: 12
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)??

Rancher
Posts: 989
9
Why do you say that k =1 is evaluated first?
What do you have as the result?

Jake Obrien
Greenhorn
Posts: 12
The result is 25.

Jake Obrien
Greenhorn
Posts: 12
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

Jake Obrien wrote:The result is 25.

and

=>

=>

=>

=> k+1 is done last

Jake Obrien
Greenhorn
Posts: 12
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

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.

Marshal
Posts: 58363
178
Where did you find that code? It just goes to show what sot of confusing code there is available.

Campbell Ritchie
Marshal
Posts: 58363
178
Yes, it is in the JLS. As EA said, along with an explanation.

Jake Obrien
Greenhorn
Posts: 12
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: 58363
178
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!