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

Duplicate ldc generated by javac

XMLWordPrintable

    • b16
    • generic
    • generic
    • Verified

        A DESCRIPTION OF THE PROBLEM :
        Given the test code provided, we would expect that
        - the call on line 20 ("".equals(newString)) evaluates to "false"
        - the call on line 21 ("A string".equals(newString)) evaluates to "true"

        Running the code, however, shows that
        - the call on line 20 evaluates to "true"
        - the call on line 21 evaluates to "false".

        It is noteworthy that the calls on line 34 and 35 evaluate to the expected values. The only difference between this two methods is that in broken(), the variable input is final, where in ok(), it is not.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and run the test code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The call on line 20 ("".equals(newString)) should evaluates to "false".
        The call on line 21 ("A string".equals(newString)) should evaluates to "true".
        ACTUAL -
        The call on line 20 ("".equals(newString)) evaluates to "true".
        The call on line 21 ("A string".equals(newString)) evaluates to "false".

        ---------- BEGIN SOURCE ----------
        import java.lang.reflect.InvocationTargetException;

        class Scratch {
            public static void main(String... args)
                    throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
                broken();
                ok();
            }

            private static void broken()
                    throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
                final var input = "A string";
                final var theClass = input.getClass();
                final var constructor = theClass.getConstructor();
                final var newString = constructor.newInstance();

                System.out.printf(
                        ("in broken(), \"\".equals(newString) = %b\n" +
                         "in broken(), \"A string\".equals(newString) = %b\n"),
                        "".equals(newString), // line 20
                        "A string".equals(newString)); // line 21
            }

            private static void ok()
                    throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
                var input = "A string";
                final var theClass = input.getClass();
                final var constructor = theClass.getConstructor();
                final var newString = constructor.newInstance();

                System.out.printf(
                        ("in ok(), \"\".equals(newString) = %b\n" +
                         "in ok(), \"A string\".equals(newString) = %b\n"),
                        "".equals(newString), // line 34
                        "A string".equals(newString)); // line 35
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        not define variable "input" as "final".

        FREQUENCY : always

              jlahoda Jan Lahoda
              pnarayanaswa Praveen Narayanaswamy
              Votes:
              0 Vote for this issue
              Watchers:
              12 Start watching this issue

                Created:
                Updated:
                Resolved: