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 Order of Evaluation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Order of Evaluation" Watch "Order of Evaluation" New topic
Author

Order of Evaluation

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
int i=7;
int k= i++ + ++i;

In C++, the value of k is undefine i.e the result may vary from compiler to compiler as the order of evaluation is not define by the Standard.

What about Java ??

Since ++ operator evaluates from Right to Left. Will they get executed as

k= i++ + ++i;

See from right k= i++(8) + ++i(8) ; // after this statement i changes to 9,(from R->Left)

Or

Take from left k= i++(7) + ++i(8) ; // i will change to 9 even here (from left to right)


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

initially the value of i is 7.

int k = i++(7) + ++i(9); is considered.

This is because the postfix expression (i++) produces a value before the variable is incremented while the prefix expression (++i) produces a value after the variable is incremented.
So in the above code.

expression (i++) has the value of 7 but the value of i is incremented to 8.
For the expression (++i) has the value of 9. Before evaluating this expression the value of i is 8 and as it returns the incremented value expression (++i) = 9

The program produces an output of 16 with this expression.


-kolli
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
@ Ravikanth kolli:

Please read the question. I know how post and pre increment operator works. . I am looking for the order of the evaluation.

You answer is incorrect. I found the solution.

++ evaluates from right to left therefore the evaluation is done from right as 8+8=16. Check above.

I have written a simple code to test whether what i think is right and yeah i was correct.

Cheers and Thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by Shivit Agarwal:
++ evaluates from right to left therefore the evaluation is done from right as 8+8=16.
Not convinced.

I think Ravikanth Kolli is correct, that is evalutes as 7 + 9. The postfix operator is applied first producing a return value of 7, then the prefix operator to the new value of 8 producing a return value of 9, then the + operator is evaluated, from left to right.
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Ok, here I goes.

int m,i=7;
int k= i++ + ++i;
System.out.println(+k +" " );

What should be the k value here ??
16 (8+8 acc. to me from Right->Left then i value gets incremented to 9)

Now modifying above code,

int m;
int k= i++ + (m=++i); // (..) will be evaluated first right ??
System.out.println(+k +" " );
k=16 as same as above.
and m=8;

Likewise i tried various combinations to check.

Now, k= i++ + ++i + 10 ; // to check whether whether 10 disrupts R->L
Heonsoo Koo
Greenhorn

Joined: Mar 31, 2008
Posts: 3
How about testing like this?
If Shivit Agarwal is right, k is 16 and l is 15.

int i = 7;
int j = 7;

int k = i++ + ++i;
int l = ++j + j++;

System.out.println("k is " + k);
System.out.println("l is " + l);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch, Heonsoo Koo. I think you and Ravikanth Kolli have both understood the execution order for the ++ operators correctly. I think you are right about printing 15 and 16 in the expressions you quote.

Why is my browser calling me eihctiR llebpbaC?? Why is the moose upside down? Is it for 1st April?

Unfortunately one cannot test the ++ operators like that, because their behaviour can only be seen within a line. If you separateinto two lines, then you can't see how they work.

Let's start with i being 7.
What i++ means is that it adds 1 to the value of i and returns the OLD value. So System.out.print(i++); prints 7. The i++ operator has the highest priority of all the operators, below only () [] and . and it associates to the right. See this Java Tutorial example and this precedence table.

The ++i operator has the precedence below i++ and as far as I can tell from this Java Language Specification page, also associates to the right. It takes the value of i and adds 1 to it and returns the NEW value.

The + operator has precedence two places lower than ++i and associates to the left. Therefore, the order of execution is:
  • 1. i is assigned the value of 7
  • 2. i is incremented (i++) to 8.
  • 3. The OLD value is applied to the subexpression to the left of the + operator. That is 7.
  • 4. The ++i operator takes the present value of i (now 8) and adds 1 to it, so i is now 9.
  • 5. The ++i operator returns the NEW value 9, so we have 9 to the right of the + operator.
  • 6. The + operator associates to the left so it takes what is on its left (7) and adds 9 to it.
  • 7. We get the result 16 to print out.
  • If you go putting (m = ++i), your () alter the order of execution.

    And the value of i++ + ++i; is defined in C and C++ to be exactly the same as in Java. There are some compilers available which are written incorrectly.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3742
        
      16
    Originally posted by Campbell Ritchie:
    Why is my browser calling me eihctiR llebpbaC?? Why is the moose upside down? Is it for 1st April?


    Yes


    Joanne
    Shivit Agarwal
    Ranch Hand

    Joined: Feb 28, 2008
    Posts: 82
    @Campell Ritchie

    I got it, the i++ precedence and the order of the evaluation confused me actually. Thanks a lot...
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    Originally posted by Shivit Agarwal:
    @Campell Ritchie

    I got it . . . Thanks a lot...
    Well done. You're welcome
     
    jQuery in Action, 2nd edition
     
    subject: Order of Evaluation