clarify semantics of postfix increment/decrement

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 8
    • Component/s: specification
    • None
    • unknown
    • generic

      wording of JLS 15.14.2 is misleading:

      "The value of the postfix increment expression is the value of the variable before the new value is stored"

      While helpful, this doesn't fully explain why programs like the following evaluates to 0:

      int result = 0;
      for (int i = 0 ; i < 100 ; i ++) {
          result = result++;
      }
      print(result); //0!!

      This behavior is consistent across all compilers - in fact the compiler simply translates this using the following JVM opcodes:

      2: iload_1 //load local variable on stack
      3: iinc 1, 1 //increment local variable (does not use stack)
      6: istore_1 //store top of stack (= unincremented value) on local variable

            Assignee:
            Alex Buckley
            Reporter:
            Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: