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

Strange error message referring to nonexistant String, upon type error

    XMLWordPrintable

Details

    • x86
    • windows_xp

    Description

      Name: js151677 Date: 08/27/2004


      FULL PRODUCT VERSION :
      java version "1.4.2_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Attempting to compile the following test case as test.java:

      public class test {
        private static int x = (new Object() + 1) + 1;
      }

      gives the expected error message for the addition of an Object and an int, but then also mysteriously complains about "incompatible types: found:java.lang.String(), required: int". Further experimentation suggests that the entire right-hand side is being interpreted as a String being assigned to the int x.

      It appears that the second plus sign coerces its left-hand-side into a String once its type cannot be determined, which rather breaks expectations - while adding an int is a valid operation for Strings, it is also valid, and more common, for other ints. Replacing the second plus sign with a different mathematical operator produces only one error, as expected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Paste the following code to a new file 'test.java':

      public class test {
        private static int x = (new Object() + 1) + 1;
      }

      and attempt to compile.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      test.java:2: operator + cannot be applied to java.lang.Object,int
        private static int x = (new Object() + 1) + 1;
                                             ^
      1 error
      ACTUAL -
      test.java:2: operator + cannot be applied to java.lang.Object,int
        private static int x = (new Object() + 1) + 1;
                                             ^
      test.java:2: incompatible types
      found : java.lang.String
      required: int
        private static int x = (new Object() + 1) + 1;
                                                  ^
      2 errors

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      See above (since expected and actual result pertain to error messages)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class test {
        private static int x = (new Object() + 1) + 1;
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None (besides the obvious "don't write buggy code").
      (Incident Review ID: 301002)
      ======================================================================

      Attachments

        Activity

          People

            mcimadamore Maurizio Cimadamore
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: