-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.1
-
sparc
-
solaris_2.5
Name: vsC45997 Date: 03/04/97
The section "4.3.1 Objects" of JLS says:
"A new class instance is implicitly created when the string concatenation
operator + is used in an expression, resulting in a new object of type String "
^^^^^^^^^^^^
However concatenation with empty string does not create the new
instance of String (JDK1.1). This is obviously optimization but it
contradicts JLS in strict sense. It would be good JLS explicitly allow
this case in 15.17.1.2. (First we had intention to file a bug against
runtime but finally decided to file it against doc subcategory.)
See some of the other appropriate excerpts:
3.10.5 String Literals
...
- Strings computed at run time are newly created and therefore distinct.
...
12.5 Creation of New Class Instances
...
- Execution of a string concatenation operator (15.17.1) that is not
part of a constant expression sometimes creates a new String object to
represent the result. String concatenation operators may also create
temporary wrapper objects for a value of a primitive type.
15.17.1.2 Optimization of String Concatenation
An implementation may choose to perform conversion and concatenation in one
step to avoid creating and then discarding an intermediate String object. To
increase the performance of repeated string concatenation, a Java compiler
may use the StringBuffer class (§20.13) or a similar technique to reduce the
number of intermediate String objects that are created by evaluation of an
expression.
See mentioned test example below:
package javasoft.sqe.tests.lang.type063.type06301;
import java.io.PrintStream;
public class type06301 {
public static void main(String argv[])
{
System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
}
public static int run(String argv[],PrintStream out) {
String s1 = "object";
String s2 = "obj";
String s3 = "ect";
String[] sa = { s1,
s2 + s3,
"" + s1,
s1 + ""
};
for ( int i = 0; i < sa.length; ++i )
{
for ( int j = 0; j < i; ++j )
{
if ( i != j )
{
if ( sa[i] == sa[j] )
{
out.println ("failed: sa["+i+"] and "+"sa["+j+
"] are the same objects" );
return 2/*STATUS_FAILED*/;
}
}
}
}
return 0/*STATUS_PASSED*/;
}
}
After compilation and execution we have:
failed: sa[2] and sa[0] are the same objects
======================================================================
- relates to
-
JDK-4858230 REGRESSION: JCK1.4a-runtime lang/TYPE/type063/type06301/type06301.html fails
-
- Closed
-