2

I came across the following on a mock exam recently, and was a bit lost as to why the answer given is 25, 25 according to the order of operations, and what I might be missing from the specification that gives the details as to why.

public class Test {

    public static void main(String[] args) {
        int k = 1;
        int[] a = {1};
        k += (k = 4) * (k + 2);
        a[0] += (a[0] = 4) * (a[0] + 2);
        System.out.println(k + " , " + a[0]);
    }
}

Just looking at line 6 above I substitute the appropriate values, and get the following:

k = k + (k = 4) * (k + 2);

I evaluate the parenthesis first, which indicates k has first been assigned to the value of 4, and subsequently is added to the number 2, giving the total of 6. This is how I interpret that:

k = k + 4 * 6;

Now this is where it gets confusing. According to the order of operations I get the following, which would be correct given the previous expression:

k = k + 24;

In my thinking at this point k should be 4 because that was the new assignment, but the answer is actually 25, and not 28. Apparently compound operators have some order of precedence I'm not understanding, or my substitution principles are not correct.

  • this is why you should always work with `final` variables, the `k` on the far left is not going to change because you change it on the right hand side. What do you think you are accomplishing with this convoluted obfuscated logic? –  Nov 17 '15 at 15:37
  • The very first sentence in the question: "I came across the following on a mock exam recently...". I would never code this garbage on purpose. –  Nov 17 '15 at 15:41
  • tell whoever is giving these kinds of questions that they are silly and impractical – ControlAltDel Nov 17 '15 at 15:42
  • Parentheses define precedence, not order of execution. For example, in `n = 1; m = n + (n = 2);`, m will equal 3, not 4. I.e. just because `(n = 2)` is in brackets doesn't mean you evaluate it first. – Klitos Kyriacou Nov 17 '15 at 15:45
  • Possible duplicate of [Java Compound Assignment Operator precedence in expressions](http://stackoverflow.com/questions/18036584/java-compound-assignment-operator-precedence-in-expressions) – jaco0646 Nov 17 '15 at 15:49
  • @ControlAltDel I would be quite happy if these types of questions were not actually on the certification exam, but I have my doubts according to what I've read. This is a question that comes from Enthuware as a part of their mock exams. –  Nov 17 '15 at 15:53

1 Answers1

6

In this answer, I will only consider the k case, it is the same for the array.

int k = 1;
k += (k = 4) * (k + 2);
// k += (k = 4) * (k + 2)
// 1 += (k = 4) * (k + 2)
// 1 +=    4    * (k + 2) with k = 4
// 1 +=    4    *   6     with k = 4
// k = 25

The tricks here:

  • k += captures the value of k before doing the calculation. += is called a compound assignment operator. Quoting the relevant part of the JLS:

    the value of the left-hand operand is saved and then the right-hand operand is evaluated.

  • k = 4 returns the assigned value, so 4.
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I think part of the key with "...the value of the left-hand operand is saved and then the right-hand operand is evaluated." is that this evaluation happens before any other order of operations on the right hand. Is that a "safe" assumption? The only reason I ask is because a big part of studying for certification is being able to correctly apply the rules, and this is one in which the rule is probably a subset of several rules. –  Nov 17 '15 at 15:46
  • 1
    @BrianReindel Yes, it is guaranteed that the value of `k` will be saved before evaluating the right hand side. Whatever you do with `k` on the right-hand side, it will always keep its previous value, which is 1 in this case. – Tunaki Nov 17 '15 at 15:48