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

Implicit conversion of char[] to String (for +) does not use Object#toString()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.1.3
    • tools
    • None
    • 1.2beta2
    • x86
    • windows_95
    • Not verified



      Name: tb29552 Date: 08/13/97


      company - Baan Labs / Baan Company N.V. , email - ###@###.###
      /*
      According to the Java Language Specification
      (First printing, August 1996) section 15.17.1.1,
      the implicit conversion from a character array
      to a String necessary for the + operator

         "is performed as if by an invocation of the
          toString method of the referenced object with
          no arguments; but if the result of invoking
          the toString method is null, then the string
          "null" is used instead."

      However, as demonstrated below, in JDK 1.1.3 the
      conversion is not done through toString but by
      interpreting a char[] as a String of characters.

      Note that the bug also shows up when the char[] is
      the first and the String is the second argument to
      the + operator.

      This bug is probably also present in JDK versions
      prior to 1.1.3, but I have not checked.
      */

      public class CharArrayBug
      {
              public static void
              main(String[] args)
              {
                      // Initialization
                      String s = "polymorph";
                      char[] ca = {'i', 's', 'm'};

                      // This concatenation goes wrong
                      String concat = s + ca;

                      // Show it
                      String concatA = s + ca.toString();
                      String concatB = s + new String(ca);
                      if (concat.equals(concatA))
                              System.out.println("Correct!");
                      else if (concat.equals(concatB))
                              System.out.println("Bug! (Expected in JDK 1.1.3.)");
                      else
                              System.out.println("Unexpected bug!!");
              }
      }
      company - Baan Labs / Baan Company N.V. , email - ###@###.###
      ======================================================================

            wmaddoxsunw William Maddox (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: