The code is as follows:
public class Example016 {public static void main (string[] args) {char c = 0x000a;/** * \u000a Note 1 *///\u000a comment 2system.ou T.println ("c =" + C); System.out.println ("A New Line");}
Results:
At compile time, note 2 error occurred, resulting in incorrect compilation.
Results Analysis:
The Java compiler not only converts Unicode escape characters into the characters they represent before parsing the program into symbols, but it does this before discarding comments and spaces. that is, when the compiler compiles the above code, it first converts the \u000a into a newline. For note 1, the comment is not affected after wrapping, and for note 2, the "Note 2" character is changed to the next line as part of the formal code, which naturally does not compile correctly.
From the use of Unicode escape characters two blog posts, we can conclude thatUnicode escape characters are absolutely confusing. Do not use Unicode escape characters unless they are indeed required. They are seldom required.
(Note: This "Java doubts" series, are bloggers read "Java doubts" original book, the original book on the explanation and example part of the adaptation, and then write a blog post. All examples are tested in person and shared on GitHub. Use these examples to motivate yourself and benefit others. At the same time, all posts in this series will be posted in the blogger's personal public number (search for "Love Ape" or "ape_it") for easy reading. If there is any infringement of the original rights of the author's content, please inform the blogger in time, in order to promptly delete, if the reader has objection to the content of the text or questions, welcome to the Blog or public messages and other ways to discuss together. )
Source code Address: Https://github.com/rocwinger/java-disabuse
This article from "Winger" blog, declined reprint!
Use of the "Java doubts" Unicode escape character (cont.)