> Last paragraph in the java.lang.String javadoc says:
>
> The Java language provides special support for the string
> concatentation operator
> ( + ), and for conversion of other objects to strings. String
> concatenation is
> implemented through the StringBuffer class and its append
> method. String
> conversions are implemented through the method toString,
> defined by Object and
> inherited by all classes in Java. For additional information
> on string concatenation
> and conversion, see Gosling, Joy, and Steele, The Java
> Language Specification.
And the Java Language Specification (Section 3.10.5: String Literals) says
this:
"Strings computed by constant expressions (§15.27) are computed at compile
time and then treated as if they were literals"
http://java.sun.com/docs/books/jls/html/3.doc.html#101083
> > Just thought that I would point out that:
> > "My " + "dog " + "has " + "fleas." will be compiled as one String:
> > "My dog has fleas." and incurs no runtime penalties. In the case
>
> Paul,
>
> Actually, my investigations in the past have shown that (at least in
> Sun's JDK 1.2) this is implemented as:
>
> new StringBuffer
> ("My").append("dog").append("has").append("fleas").toString();
If this is actually the case, Sun's JDK is not in compliance with the spec.
However, in my tests, this is not the case.
>From this class:
public class StringTest {
static String blah = "My " + "dog " + "has " + "fleas.";
}
The following is the result from "javap -c StringTest" after compiling:
Compiled from StringTest.java
public class StringTest extends java.lang.Object {
static java.lang.String blah;
static {};
public StringTest();
}
Method static {}
0 ldc #1 <String "My dog has fleas.">
2 putstatic #5 <Field java.lang.String blah>
5 return
Method StringTest()
0 aload_0
1 invokespecial #4 <Method java.lang.Object()>
4 return
regards,
michael
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]