关于unicode:为什么我不能使用

Why can't I use u000D and u000A as CR and LF in Java?

为什么我不能在Java中使用U000 0D和U000 0A作为CR和LF?编译代码时出错:

1
illegal line end in character literal


unicode转义在编译器运行之前经过预处理。因此,如果将\u000A放在这样的字符串文字中:

1
String someString ="foo\u000Abar";

它的编译方式与您所写的完全相同:

1
2
String someString ="foo
bar"
;

坚持
(回车;0x0D)和
(换行;0x0A)

额外好处:你可以一直享受这个乐趣,特别是考虑到大多数语法强光灯的限制。下次有时间,请尝试运行以下代码:

1
2
3
4
5
6
7
public class FalseIsTrue {
    public static void main(String[] args) {
        if ( false == true ) { //these characters are magic: \u000a\u007d\u007b
            System.out.println("false is true!");
        }
    }
}


因为它属于Unicode控制字符的范围

它是U+0000–U+001FU+007F

Unicode control characters are used to control the interpretation or
display of text, but these characters themselves have no visual or
spatial representation.

它们可以通过使用上述答案中所述的\通过@mark进行转义。

来自RFC:

2.5. Strings

The representation of strings is
similar to conventions used in the C
family of programming languages. A
string begins and ends with quotation
marks. All Unicode characters may be
placed within the quotation marks
except for the characters that must be
escaped: quotation mark, reverse
solidus, and the control characters
(U+0000 through U+001F).

Any character may be escaped.