Java Puzzle characters (puzzle 16)

Source: Internet
Author: User

Puzzle 16: Line Print Program

The row delimiter is the name of the character or string that splits the line of text, and it differs on different platforms. On the Windows platform, it consists of the CR character (carriage return) and the LR (line-wrapping) character immediately followed. On UNIX platforms, a separate LF character is typically referenced as a newline character. So, this time the puzzle is also derived from the line delimiter, to see the following that the LF character passed to the Println method of the program will print what, its behavior depends on the platform?

public class Lineprinter{public static void Main (string[] args) {//note:\u000a is Unicode representation of linefeed (LF) ch Ar c = 0x000A; System.out.println (c);}}

In fact, the behavior of this program is platform-Independent: it can not be compiled on any platform. If you try to compile it, you will get an error message similar to the following: '; ' expected//note:\u000a is Unicode representation of Lintfeed (LF) 1 Error

See this maybe everyone will be familiar with, yes, this is the same as the puzzle 15 made the same mistake, in the third line of comments, \u000a is a Unicode escape character, the puzzle 15 has introduced the Unicode escape character, it is not clear to see the puzzle 15, then before the compiler discards the comment , the Unicode escape character is escaped to a line delimiter in a timely manner, that is, the code that will be compiled after escaping is:

public class Lineprinter{public static void Main (string[] args) {//note:is Unicode representation of linefeed (LF) char c = 0 x000a; System.out.println (c);}}

It is clear that such code is not compiled, the simplest way to fix the program is to remove the Unicode escape character in the comment, but doing so may cause the program reader to be confused about what the code wants to say, So a better way is to initialize the line delimiter directly with an escape character sequence instead of a hexadecimal integer literal, so that you can clearly express the contents of the program, so that no extra annotations are needed.

public class Lineprinter{public static void Main (string[] args) {char c = ' \ n '; System.out.println (c);}}

As long as you do this, the program will be able to ensure that the compilation pass and run, but this is still a problematic program, the behavior of the previously uncorrected program (that is, a compilation error) is platform-independent, then the revised program behavior is related to the platform, the reason is the puzzle to remind. On some platforms, such as UNIX, it will print two full line separators, but it will not behave like this on other platforms, such as Windows. Although these outputs are the same as the naked eye, they can easily cause problems if they are stored in a file or exported to a subsequent handler. If you want to print two lines of blank lines, you should honestly call the Println method two times. If you happen to use JDK 5.0, you can also use the format string "%n%n" for printf instead of println. Every occurrence of%n causes printf to print an appropriate, platform-related line delimiter.

With Puzzle 14, puzzle 15, and this puzzle, we all know that Unicode escape characters are absolutely confusing. Therefore, we do not use Unicode escape characters unless they are indeed required.

Java Puzzle characters (puzzle 16)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.