Java Reference
In-Depth Information
A string literal cannot be broken into two lines.
"He
llo" // Cannot continue "He in this line. A compiler error
If you want to break
"Hello"
in two lines, you must break it using the string concatenation operator (
+
), as shown:
"He" +
"llo"
or
"He"
+ "llo"
Another example of a multi-line string literal is shown below. The entire text represents a string literal.
"This is a big string literal" +
" and it will continue in several lines." +
" It is also valid to insert multiple new lines as we did here. " +
"Adding more than one line in between two string literals " +
"is a feature of Java Language syntax, " +
" not of string literal."
Escape Sequence Characters in String Literals
A string literal is composed of characters. It is valid to use all escape sequence characters to form a string literal. For
example, to include a line feed and a carriage return characters in a string literal you will use
\n
and
\r
, as shown:
"\n" // A string literal with a line feed
"\r" // A string literal with a carriage return
"\n\r" // A string literal with a line feed and a carriage return
"First line.\nSecond line." // An embedded line feed
"Tab\tSeparated\twords" // Embedded tab escape characters
"Double quote \" is here" // An embedded double quote in string literal
Unicode Escapes in String Literals
A character can also be represented as a Unicode escape in the form
\uxxxx
, where an
x
is a hexadecimal digit
(
0-9
or
A-F
). In a string literal, the character '
A'
, the first uppercase English letter, can also be written as '
\u0041'
, for
example,
Apple
and
\u0041pple
are treated the same in Java. Line feed and carriage return escape characters can also
be represented in Unicode escape character as '
\u000A'
and '
\u000D'
, respectively. You cannot use Unicode escapes
to embed a line feed and a carriage return characters in string literals. In other words, you cannot replace
'\n'
with
'\u000A'
and
'\r'
with
'\u000D'
in a string literal. Why? The reason is that Unicode escapes are processed in the
very beginning of the compilation process resulting in the conversion of '
\u000A'
and '
\u000D'
into a real line feed,