網頁

2010年4月6日 星期二

Evaluation Order

Evaluation Order

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.


/**
 * expression evaluation order test
 */

public class EvaluationOrder {
    public static void main(String [] args) {
        int i = 2;
        int j = i * (i=3);
        System.out.println(j);
    }
}

/*

$ java EvaluationOrder
6

*/


/*
 *  expression evaluation order test
 */

int main( void )
{
    int i = 2;
    int j = i * (i=3);
    printf("%d\n", j);
}

/*
gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

$ ./EvaluationOrder.exe
9

*/

沒有留言: