Question

Consider this function:

public static final int F(int a, int b) {
    a = a - 1 + b;
    // and some stuff
    return a;
}

Is it required for implementations of JVMs to execute - 1 before + b?

If we have a system profiler attached to the JVM, will we see the + b operation being carried out before the + 1 operation?

Was it helpful?

Solution

Actually, I will disagree with the rest of the answers. The JLS §15.7 that people are referring to discusses the evaluation of operands. That is, in the expression

x = foo() - 1 + bar()

, in which order will the methods be invoked.

The relevant section is §15.7.3, which specifies

An implementation may not take advantage of algebraic identities such as the associative law to rewrite expressions into a more convenient computational order unless it can be proven that the replacement expression is equivalent in value and in its observable side effects [...]

Since the expression x = x - 1 + q is equivalent in all ways to x = x + q - 1, a conforming implementation is allowed to rewrite the expression (if it for some reason should decide that is more efficient).

OTHER TIPS

Yes, it's in the Java language specification, §15.7.

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.

According to here:

Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

So yes.

Yes, although the bigger question is does it really matter? Subtraction and addition have the same precedence in any scientific mathematical operation and are commutable. That is:

x = input - 1 + q;

is the same as

x = input + q - 1;

Yes:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7

The JVM will intepret

x = input - 1 + q;

as

x = (input - 1) + q;

You can see here the operator precedence in Java: http://bmanolov.free.fr/javaoperators.php . Because + and - have the same precedence, they will be executed in the order in which they appear.

If you want to be more clear about what which operation takes place first (or if you want to break the built-in precedence), you can (and should) use parantheses (( and )), like this:

x = (a - b) + (c * (d - e))

It would actually not be the JVM implementation that matters, since they just execute a list of instructions from the class file.

Yes it always will, even though it wouldn't affect the result of adding / subtracting anyway.

It seems to me there will always be a side effect from executing X + q - 1 When you should be executing X - 1 + q.

When the sum x + q exceeds Bignum by 1, the left to right execution will succeed... The out of order execution will generate an overflow.

So the out of order execution has a side-effect.

Unless the out-of-order execution traps that overflow itself, then re-do es the calculation in the correct order when necessary to avoid the side effect.

Important to add here is that java does have in line precedence for operators which appears to be based on the basic arithmetic order of operations. For a list of full precedence look at this source

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top