programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Operator precedence

Jake Obrien
Greenhorn
Posts: 12
Hi, I was wondering if someone could shed some light on a small problem that I can't seem to get my head around.

Output: 6

Should this not equal 7???

If we expand the query k = k + 3 + ++k
From what I can see with order of operator perference ++k should be the first part evaluated.
If k = 1 then ++k = 2. So, should the query not then read. k = k + 3 + 2.
As k is now equal to 2, if we sub this value into the expression k = 2 + 3 + 2.
Should k not equal 7 then?

First post here by the way, sorry if I have posted in the wrong section.

Greg Charles
Sheriff
Posts: 3015
12
Hi Jake, welcome to JavaRanch!

I've seen different permutations of these issues and they're always sort of confusing. If I remember correctly, what's gong on is that k's value (1) is evaluated and stored somewhere, then k is incremented (to 2) and added to 3 (making 5) and then that is added to the stored value of k (making 6) and the result is assigned back to k.

Campbell Ritchie
Marshal
Posts: 56587
172
Jake Obrien wrote: . . .

Should this not equal 7???
. . .
Welcome again. I went through it and got 6.
The m1 method returns 1
You add 3 and (1 incremented by 1 makes 2) to that. I still think that is 6.

I think this suggestion in our style guide is excessive, and you can use i++ or --i in expressions, but you can see what confusing code you get. People put such confusing code in exams to see whether candidates understand the ++ and -- operators.

Jake Obrien
Greenhorn
Posts: 12
Thanks guys.
Still dont think this is right.

if k += 3 + ++k;

If expanded does this not equal k = k + 3 + ++k?
If m1 returns 1 - "you add 3 and (1 incremented by 1 makes 2) to that. I still think that is 6. "

Do you still have to add k?

i.e k = 2 + 3 + 2?

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
No. Evaluation is from left to right. k is evaluated before it's incremented on the right. So:

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Discussion: do you think operands should be evaluated when their surrounding expression is evaluated, or when they are encountered?

Campbell Ritchie
Marshal
Posts: 56587
172
Interesting question. That is why you get conflicting results in languages like C, where it is not strictly defined. But Java defines it strictly as left to right (beware: that is a JLS link and they JLS ain't easy reading).

If you search these fora, you will find out that similar questions occur frequently.

Jake Obrien
Greenhorn
Posts: 12
Thanks guys. Cheers Stephen that makes perfect sense now.
Studying for the OCA exam... Going through some sample question and just couldn't get my head around this one.

Campbell Ritchie
Marshal
Posts: 56587
172
You're welcome

 It is sorta covered in the JavaRanch Style Guide.