Java Reference
In-Depth Information
< Day Day Up >
Puzzle 16: Line Printer
The line separator is the name given to the character or characters used to separate lines of text,
and varies from platform to platform. On Windows, it is the CR character (carriage return) followed
by the LF character (linefeed). On UNIX, it is the LF character alone, often referred to as the
newline character. The following program passes this character to println . What does it print? Is
its behavior platform dependent?
public class LinePrinter {
public static void main(String[] args) {
// Note: \u000A is Unicode representation of linefeed (LF)
char c = 0x000A;
System.out.println(c);
}
}
Solution 16: Line Printer
The behavior of this program is platform independent: It won't compile on any platform. If you tried
to compile it, you got an error message that looks something like this:
LinePrinter.java:3: ';' expected
// Note: \u000A is Unicode representation of linefeed (LF)
^
1 error
If you are like most people, this message did not help to clarify matters.
The key to this puzzle is the comment on the third line of the program. Like the best of comments,
 
 
Search WWH ::




Custom Search