String Literals
A string literal is a sequence of characters that must be enclosed in double quotes and must occur on a single line. All string literals are objects of the class String (§8.4, p. 439).
Escape sequences as well as Unicode values can appear in string literals:
“Here comes a tab.\t And here comes another one\u0009!” (1)
“What’s on the menu?” (2)
“\”String literals are double-quoted.\”” (3)
“Left!\nRight!” (4)
“Don’t split (5)
me up!”
In (1), the tab character is specified using the escape sequence and the Unicode value, respectively. In (2), the single quote need not be escaped in strings, but it would be if specified as a character literal (‘\”). In (3), the double quotes in the string must be escaped. In (4), we use the escape sequence \n to insert a newline. The expression in (5) generates a compile-time error, as the string literal is split over several lines. Printing the strings from (1) to (4) will give the following result:
Here comes a tab. And here comes another one !
What’s on the menu?
“String literals are double-quoted.”
Left!
Right!
One should also use the escape sequences \n and \r, respectively, for correct interpretation of the characters \u000a (newline) and \u000d (form feed) in string literals.
Whitespace
Whitespace is a sequence of spaces, tabs, form feeds, and line terminator characters in a Java source file. Line terminators include the newline, carriage return, and carriage return–newline sequence.
A Java program is a free-format sequence of characters that is tokenized by the compiler—that is, broken into a stream of tokens for further analysis. Separators and operators help to distinguish tokens, but sometimes whitespace has to be inserted explicitly as a separator. For example, the identifier classRoom will be interpreted as a single token, unless whitespace is inserted to distinguish the keyword class from the identifier Room.
Whitespace aids not only in separating tokens, but also in formatting the program so that it is easy to read. The compiler ignores the whitespace once the tokens are identified.