15.17.1
String Concatenation Operator
+
EXPRESSIONS
Now only reference values need to be considered. If the reference is
null
, it is
converted to the string "
null
" (four ASCII characters
n
,
u
,
l
,
l
). Otherwise, the
conversion is performed as if by an invocation of the
toString
method of the ref
erenced object with no arguments; but if the result of invoking the
toString
method is
null
, then the string "
null
" is used instead. The
toString
method
( 20.1.2) is defined by the primordial class
Object
( 20.1); many classes override
it, notably
Boolean
,
Character
,
Integer
,
Long
,
Float
,
Double,
and
String
.
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.
For primitive objects, an implementation may also optimize away the creation
of a wrapper object by converting directly from a primitive type to a string.
15.17.1.3
Examples of String Concatenation
The example expression:
"The square root of 2 is " + Math.sqrt(2)
produces the result:
"The square root of 2 is 1.4142135623730952"
The + operator is syntactically left associative, no matter whether it is later
determined by type analysis to represent string concatenation or addition. In some
cases care is required to get the desired result. For example, the expression:
a + b + c
is always regarded as meaning:
(a + b) + c
Therefore the result of the expression:
1 + 2 + " fiddlers"
is:
"3 fiddlers"
but the result of:
"fiddlers " + 1 + 2
356
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved