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

+= narrowing conversion

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 17
    • Fix Version/s: None
    • Component/s: tools
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Javac 17.0.1 running on Windows 11

      A DESCRIPTION OF THE PROBLEM :
      Given int n = 2; the statement n = n + 1.1; generates a narrowing conversion compilation error,
      The statement n += 1.1; does not generate an error. It was my understanding that the two statements should do the same thing.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Given int n = 2;
      The statement n = n + 1.1; generates a narrowing conversion compilation error,
      The statement n += 1.1; does not generate an error

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both statement should be equivalent and should both generate narrowing conversion errors.
      ACTUAL -
      int n = 2; n += 1.1; produces a value of 3 in n.

      ---------- BEGIN SOURCE ----------
      public class NarrowTest {
         public static void main(String[] unused) {
            int n = 2;
            n += 1.1;
            System.out.println(n);
         }
      }

      public class NarrowTest {
         public static void main(String[] unused) {
            int n = 2;
            n = n + 1.1;
            System.out.println(n);
         }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One can simply write the code differently.

        Attachments

          Activity

            People

            Assignee:
            adev ANUPAM DEV
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: