JDK-4959686 : (spec) x = "a" + 4 + "c" example in StringBuffer documentation is misleading
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-11-25
  • Updated: 2004-11-03
  • Resolved: 2004-11-03
Related Reports
Duplicate :  
Description
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

Comments
EVALUATION Not for tiger beta 1. ###@###.### 2003-12-03 It appears that this documentation was removed from StringBuffer as part of the putback for this bug: 4261803: Need an unsynchronized StringBuffer These changes were in tiger-b14 (SCCS update 1.82.1.1) which was prior to tiger-beta1. Closing this bug as duplicate of 4261803. ###@###.### 11/3/04 23:09 GMT
03-11-0004