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

(spec) x = "a" + 4 + "c" example in StringBuffer documentation is misleading

XMLWordPrintable

    • x86
    • windows_2000

      Name: jl125535 Date: 11/25/2003


      URL OF FAULTY DOCUMENTATION :
      http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html

      A DESCRIPTION OF THE PROBLEM :
      The documentation for StringBuffer contains:

      For example, the code:
      x = "a" + 4 + "c" is compiled to the equivalent of: x = new StringBuffer().append("a").append(4).append("c").toString()

      This is not correct.
      "a" + 4 + "c" is a concatenation of constants, and so the concatenation is done at compile time (JLS - 3.10.5 String Literals at http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101083):
      ("a" + 4 + "c") == "a4c"
      System.out.println(("a" + 4 + "c") == "a4c");

      Since this is the case, x = "a" + 4 + "c", concatenation occurs at compile time, the code is not the equivalent of x = new StringBuffer().append("a").append(4).append("c").toString().

      Sure the final result is the same (a String variable x has the value "a4c"), but the two methods of getting there are not equivalent.
      (Incident Review ID: 185550)
      ======================================================================
      ###@###.### 11/3/04 23:09 GMT

            iris Iris Clark
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: