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 .
A string literal is always of type String 4.3.3 ).
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-
sibly parenthesized) expression using the string concatenation operator + 15.18.1 ) .
The following are examples of string literals:
Click here to view code image
"" // 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,
one should write "\n" 3.10.6 ) . Similarly, it is not correct to write "\u000d" for a string
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 (“).
A string literal is a reference to an instance of class String 4.3.1 , § 4.3.3 ).
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
15.28 ) - are “interned” so as to share unique instances, using the method String.intern .
Search WWH ::




Custom Search