File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Operator Precedence x = a++ + b++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Operator Precedence x = a++ + b++ " Watch "Operator Precedence x = a++ + b++ " New topic
Author

Operator Precedence x = a++ + b++

Ros Bain
Greenhorn

Joined: Nov 16, 2005
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

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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ros Bain
Greenhorn

Joined: Nov 16, 2005
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;



Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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!!



A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
Steve Morrow
Ranch Hand

Joined: May 22, 2003
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

Joined: May 22, 2003
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

Joined: Nov 16, 2005
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!)

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

Thanks for all your help on this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator Precedence x = a++ + b++