Java Reference
In-Depth Information
< Day Day Up >
Puzzle 15: Hello Whirled
The following program is a minor variation on an old chestnut. What does it print?
/**
* Generated by the IBM IDL-to-Java compiler, version 1.0
* from F:\TestRoot\apps\a1\units\include\PolicyHome.idl
* Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00
*/
public class Test {
public static void main(String[] args) {
System.out.print("Hell");
System.out.println("o world");
}
}
Solution 15: Hello Whirled
This puzzle looks fairly straightforward. The program contains two statements. The first prints
Hell
and the second prints
o world
on the same line, effectively concatenating the two strings.
Therefore, you might expect the program to print
Hello world
. You would be sadly mistaken. In
fact, it doesn't compile.
The problem is in the third line of the comment, which contains the characters
\units
. These
characters begin with a backslash (
\
) followed by the letter
u
, which denotes the start of a Unicode
escape. Unfortunately, these characters are not followed by four hexadecimal digits, so the Unicode
escape is ill-formed, and the compiler is required to reject the program.
Unicode escapes must be
well formed, even if they appear in comments.
It is legal to place a well-formed Unicode escape in a comment, but there is rarely a reason to do so.
Programmers sometimes use Unicode escapes in Javadoc comments to generate special characters
in the documentation:
Search WWH ::
Custom Search