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

Section 15.26.2 iin JLS 3 incorrectly allows += to be applied to Object, String.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P3
    • None
    • 6
    • specification
    • x86
    • windows_xp

    Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      JLS 2: 15.26.2 begins:

      "All compound assignment operators require both operands to be of primitive type, except for +=, which allows the right-hand operand to be of any type if the left-hand operand is of type String.

      A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion (§5.1.1) or a narrowing primitive conversion"


      while JLS 3: 15.26.2 begins with only:

      "A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once"


      In addition to the intended purpose of the change (allowing primitive wrappers to be used), this would also allow:

      Object o = new Object();
      o += "test";

      since:

      Object o = new Object();
      o = (Object)((o) + ("test"));

      is legal. The former is not accepted by javac.



      REPRODUCIBILITY :
      This bug can be reproduced always.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: