Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

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

Why can't I use u000D and u000A as CR and LF in Java? It's giving an error when I compile the code:

String x = "u000A hello";//Error - Illegal escape character in string literal.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unicode escapes are pre-processed before the compiler is run. Therefore, if you put u000A in a String literal like this:

String someString = "foou000Abar";

It will be compiled exactly as if you wrote:

String someString = "foo
bar";

Stick to (carriage return; 0x0D) and (line feed; 0x0A)

Bonus: You can always have fun with this, especially given the limitations on most syntax highlighters. Next time you've got a sec, try running this code:

public class FalseIsTrue {
    public static void main(String[] args) {
        if ( false == true ) { //these characters are magic: u000au007du007b
            System.out.println("false is true!");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...