Forums Register Login

Operator Precedence x = a++ + b++

+Pie Number of slices to send: Send
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

The answer given is always
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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;



+Pie Number of slices to send: Send
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!!

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Ros Bain == ashok reddy devaram ???

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


I thought this looked terribly familiar...
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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!)

The information about the use of the stack was very helpful.

Thanks for all your help on this.
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6902 times.
Similar Threads
Operator Presendence
Conditional Operators (try this)
Operator Precedence qstn.
Operator evaluation order
Increment operator in a mock exam
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:26:41.