programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers 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 x = a++ + b++

Greenhorn
Posts: 4
In many of the mock SCJP exams the following type of question occurs

int x, a = 6, b = 7;
x = a++ + b++;

After execution of the code fragment above what are the values of x,a and b

a = 7, b= 8 and x = 13
This is because x is evaluated as a+b and then a++ and b++ are evaluated.

I don't understand why!
According to the operator precedence rules in my java manual unary operators (++) take precedence over arithmetic operators (+).
Is there some other special rule which applies for this case - it appears that the + operator is taking precedence over the ++ operator.

ranger
Sheriff
Posts: 17314
11
But, what that doesn't state is the x++ is sort of like calling a function with a return value, and that is the old value of x, not what x++ evaulates to.

so

int x = 6;
int y = 7;

int a = x++ + y++;

so x = 6 then x++ returns the old value of x which is 6 though after it it will be 7, since y = 7 then y++ returns the old value of y which is 7 though after it it will be 8.

so x = 7, y = 8 and a = 13.

Mark

Ros Bain
Greenhorn
Posts: 4
Thanks - I forgot about the importance of the position of the ++

In order for x to evaluate as 15 I would have needed the expression to read

x = ++a + ++b;

(instanceof Sidekick)
Ranch Hand
Posts: 8791
Another good answer to this dilema is "Don't do that!" This is a useful learning exercise as you showed by coming up with the ++a + ++b (good work, by the way) but not something I'd want to run across on a dark night. Even the most experienced Java users find mixing unaries and other operators takes a few too many brain cycles to process. Make it easy for humans first and foremost!!

Mark Spritzler
ranger
Sheriff
Posts: 17314
11
Yeah, try to figure out what this code evaluates too, and run away.

int x=3;
int y = x++ + ++x;

What is y and what is x after the statements run.

Mark

Ranch Hand
Posts: 657
Ros Bain == ashok reddy devaram ???

http://www.coderanch.com/t/251647/java-programmer-SCJP/certification/Why-so

I thought this looked terribly familiar...

Steve Morrow
Ranch Hand
Posts: 657

What is y and what is x after the statements run.

While I wouldn't expect it from a "beginner", that code's not so bad after the discovery of the difference between the variable array and operand stack...

Ros Bain
Greenhorn
Posts: 4
int x=3;
int y = x++ + ++x;

1) evaluate x++, and push the value onto the stack
vars: {y, 3} stack: {3}

2) increment x
vars: {y, 4} stack: {3}

3) evaluate ++x and push the value onto the stack
vars: {y,4} stack: {4}

4) increment x
vars: {y, 5} stack: {4}

5) add the value of x from the stack to the value of x from the stack, and store the result in y
vars: {8, 5} stack: {}

So y is 8 and x is 5

I would definitely not recommend writing this sort of code as it's confusing for people to understand. For the java exam you need to be able to interpret code statements like this in case you should come across them in other peoples code.

(By the way Ros Bain != ashok reddy devaram
However I notice he has a very very similar taste in questions to me - very curious!)