Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8153718

Post-and-Pre-unary operators (expr++ and ++expr) precedence

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_73"
      Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [versão 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The order of precedence of operators is not being respected when used on a statement with multiplication. For the case in question, ++expr is being evaluated before expr++. However, expr++ has higher precedence over ++expr (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      assign to a int variable (x) the value 10;
      assign to a int variable (y) the value x++ * ++x * x++;


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      y = 10 * 13 * 11
      y = 1430
      ACTUAL -
      y = 1440

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) {
          int x = 10;
          int y = x++ * ++x * x++;
          System.out.println(y);
      }
      ---------- END SOURCE ----------

            aroy Abhijit Roy (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: