Java Reference
In-Depth Information
"
StringCharacters
opt
"
StringCharacters:
StringCharacter
StringCharacters StringCharacter
StringCharacter:
InputCharacter
but not
"
or
\
EscapeSequence
See §
3.10.6
for the definition of
EscapeSequence
.
It is a compile-time error for a line terminator to appear after the opening
"
and before the
closing matching
"
.
As specified in §
3.4
, the characters CR and LF are never an
InputCharacter
; each is
recognized as constituting a
LineTerminator
.
A long string literal can always be broken up into shorter pieces and written as a (pos-
The following are examples of string literals:
"" // the empty string
"\"" // a string containing " alone
"This is a string" // a string containing 16 characters
"This is a " + // actually a string-valued constant expression,
"two-line string" // formed from two string literals
Because Unicode escapes are processed very early, it is not correct to write
"\u000a"
for a string literal containing a single linefeed (LF); the Unicode escape
\u000a
is trans-
formed into an actual linefeed in translation step 1 (§
3.3
) and the linefeed becomes a
LineTerminator
in step 2 (§
3.4
), and so the string literal is not valid in step 3. Instead,
literal containing a single carriage return (CR). Instead, use
"\r"
. Finally, it is not pos-
sible to write
"\u0022"
for a string literal containing a double quotation mark (“).
Moreover, a string literal always refers to the
same
instance of class
String
. This is because
string literals - or, more generally, strings that are the values of constant expressions