0

Let's take the following code:

int[] data   = {1, 2, 3};
int   pos    = 0;
int   result = data[pos++] + data[pos++]*data[pos++];

Am I guaranteed that my result is always 1 + 2*3, i.e. 7, or could a Java compiler or JVM reorder the execution of the data[pos++] statements such that I, for example, get 3 + 1*2, i.e. 5, since multiplication has priority over addition?

In other words: In a single statement containing multiple ++-operations on the same variable, am I guaranteed that they are always executed left to right?

Markus A.
  • 12,349
  • 8
  • 52
  • 116
  • [I'm always losing my keys.](http://meta.stackexchange.com/questions/215379/should-drive-by-downvoting-be-more-effectively-caught/215397#215397) – Sotirios Delimanolis Oct 06 '15 at 01:00
  • I'm not one of the downvoters but I don't understand very well your question. If you want to see the priority in Java of the operators you can see it here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html – Francisco Romero Oct 06 '15 at 01:01
  • @SotiriosDelimanolis Actually, this question is different from the one you linked. The one you linked asks about the order of the right-hand-side of the assignment being evaluated before the left-hand side. My question is only concerned with the right-hand-side. Also, in the question you linked, any reordering of the `x++` terms would yield the same answer due to the commutative property of the multiplies operation. – Markus A. Oct 06 '15 at 01:06
  • I up voted because I thought the question showed a genuine curiosity about the language. If you had shown me the code and asked for the result, I could not have computed, but I could have said more generally "It will always be what it is". There really is not anything in the sample code which is complex enough to expect a non-deterministic result. – Tim Bender Oct 06 '15 at 01:08
  • 1
    Please edit that into your question if you feel that way. That's how your question will be added to the re-open queue. Now, the operand doesn't matter and neither does the assignment part of it. You can ignore the `+=` by removing one evaluation of the variable. As the quote from the JLS states, operands must be fully evaluated. Also, _this is well defined in Java and will behave the same on each invocation in any runtime._ – Sotirios Delimanolis Oct 06 '15 at 01:09
  • @TimBender Not sure I fully understand your comment. The complexity could come in if the JVM, for example, decides to evaluate `data[pos++]*data[pos++]` first (for example to save a register when doing the calculation), in which case it could turn into `data[2] + data[0]*data[1]`... – Markus A. Oct 06 '15 at 01:09
  • Or perhaps in the java compiler to byte code? To rephrase your question "Does the Java Language Specification include rules on Operator Precedence" and the answer is "Yes". Since it is clearly defined the outcome "will always be what it is". – Tim Bender Oct 06 '15 at 01:11
  • Maybe [this](http://stackoverflow.com/questions/32578869/confusion-using-increment-and-decrement-operators-in-java) is a better duplicate. – Sotirios Delimanolis Oct 06 '15 at 01:13

2 Answers2

1

Quoting Java Language Specification:

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

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

In Java, this is guaranteed.

From the Java Language Specification, section 15.7.1:

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

This implies that in a+b, a is evaluated before b, and in x*y x is evaluated before y.

You have an expression of the form p + (q * r). By the above rule, p must be evaluated before (q * r). And when evaluating q * r, q must be evaluated before r. So p must be evaluated, then q, then r.

(Note that this behaviour differs from C and C++)

user253751
  • 57,427
  • 7
  • 48
  • 90