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

Postfix increment operator within lambda is not evaluated as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8
    • tools
    • generic
    • generic

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When a postfix increment expression appears within the body of a lambda, it evaluates in the same way as a prefix increment expression.

      Take for example


      The output of this program is:

      anonymous class returns: 0
      lambda returns: 1

      where you would expect it to return 0 in both cases.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the code provided below. Inspect the output.




      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      We would expect the following output

      anonymous class returns: 0
      lambda returns: 0

      since

      t -> t++;

      should be equivalent to

      t -> {return t++;};


      ACTUAL -
      The following output

      anonymous class returns: 0
      lambda returns: 1

      which shows that the value of the postfix decrement expression is the value of the variable after the new value is stored.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Example {
      public static void main(String[] args) throws Exception {
      Supplier<Integer> s1 = new Supplier<Integer>() {
      @Override
      public Integer get(Integer t) {
      return t++;
      }
      };
      System.out.println("anonymous class returns: " + s1.get(0));

      Supplier<Integer> s2 = t -> t++;
      System.out.println("lambda returns: " + s2.get(0));
      }
      }

      interface Supplier<T> {
      T get(T t);
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: