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
• Tim Cooke
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• paul wheaton
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Ganesh Patekar
Bartenders:
• Tim Holloway
• Carey Brown
• salvin francis

# Order of operations with operators

Ranch Hand
Posts: 35
Hi,

I am a little unsure of my understanding of what is going on here and why.

k = 1;
k += k = 4 * k + 2;
output is 7.

is this what is happening?
k += k = (4 * k) + 2;

I understand the basic concept that * happens before +. Does it also work before the = assignment? I know this is a very basic concept but when I see more than one assignment operator it confuses me a little still.

Ranch Hand
Posts: 434
Let me give it a shot:

k = 1;
k += k = 4 * k + 2;

k += k = (4 * 1) + 2; // because * has greater precedence than +
k += k = 4 + 2; // expressions are evaluated from right to left
k += k = 6; // left-most k still has the original value as 1
k += 6; // 1 + 6 is 7
k = 7;

It take a while to get your head wrapped around these expressions

Saloon Keeper
Posts: 10507
224
What Sandra says is correct.

The trick is that even though the results of operations depend on order of precedence, variables are always evaluated from left to right. It should be more clear when we write the += operator out completely:

Greenhorn
Posts: 25
Hello,

Could you please elaborate more why after k += k = 6; the left-most k still has the original value as 1 ? I thought that after assignment k = 6 it would change the value to 6 so it will be 6+6 not 1+6.

Thanks.

Stephan van Hulst
Saloon Keeper
Posts: 10507
224
Because variables are evaluated before calculations are done with them, and they are evaluated from left to right, as they are needed.

The += and = operators have the same precedence but they are right-associative, meaning they are performed from right to left. However, the variables are still evaluated from left to right, and this is done before any calculations.

k += k = 6 is shorthand for k = (int) k + (k = 6). Since the k = 6 has a higher precedence than the addition, 6 gets assigned to k before it is added to k. However, the *value* used in the addition on the left side, is already evaluated before the assignment is done, so what actually happens is this:

k = (int) 1 + (k = 6).

Ranch Hand
Posts: 300

great explanation.

Always remember while evaluating expression two things comes in picture

1) Precedence of operation ( higher will be executed first)
2) Associativity (either right to left or left to right)

Also the approach shown here is quite good (solving expression by breaking them and starting it from right most)

Cheers
Javin

Ranch Hand
Posts: 58
This topic is very interesting. I really learn a lot. Happy new year everyone.

Kamila Jolan
Greenhorn
Posts: 25
Stephan thanks for explanation I think i almost got it ,
but then why if I wrote that code:

the value 2 is inserted in the position 0 in the array, I would say that this is happening:

but the value is 2 not 6
Thanks

Stephan van Hulst
Saloon Keeper
Posts: 10507
224
Because evaluation happens only when it is needed. This is a difficult concept to wrap your head around at first. Here goes:

author
Posts: 23838
141

Javin Paul wrote:
Always remember while evaluating expression two things comes in picture

1) Precedence of operation ( higher will be executed first)
2) Associativity (either right to left or left to right)

Also the approach shown here is quite good (solving expression by breaking them and starting it from right most)

Cheers
Javin

There is actually three "things" that needs to "comes in picture". The operator precedence is used to determine grouping, as if parens were added. It is actually not used to determine what will be "executed first". The assoc clarifies the precedence, for operators with the same precedence.

And finally, there is the order of evaluation. This is defined in the specification, and it is independent of the operator precedence. Normally, you don't need to know about the evaluation order; and understanding the operator precedence is good enough. When it needs to be understood, is when there are side-effects to the expression, when the actual evaluation order is important... expressions with assignments whose value is used, post and prefix operators, etc., fall within this category.

Henry

Stephan van Hulst
Saloon Keeper
Posts: 10507
224
I edited my above example since Henry's post.

author
Posts: 8995
19
Hey Guys,

Great topic, but it's a little bit over the top for the real exam.

Older versions of the exam put emphasis on this kind of intricate precedence discussion, but the 5 and 6 exams don't. You might encounter one question that covers a little bit of this, but for the most part on the newer exams, the exam team decided not to focus on that (you'll see lots of parenthesis to make the order of operation clear.)

So, my recommendation is this:

- treat this as a a low priority topic
- if you have all the more important topics completely, absolutely under control, then you could choose to study this.
- in the real world, use parenthesis

hth,

Bert

Stephan van Hulst
Saloon Keeper
Posts: 10507
224

Bert Bates wrote:- in the real world, use parenthesis

- in the real world, don't embed compound assignment operators in larger expressions.

 I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness! create, convert, edit or print DOC and DOCX in Java https://products.aspose.com/words/java