为什么我不能在Java中使用\ u000D和\ u000A作为CR和LF? [英] Why can't I use \u000D and \u000A as CR and LF in Java?

查看:2669
本文介绍了为什么我不能在Java中使用\ u000D和\ u000A作为CR和LF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在Java中使用\ u000D和\ u000A作为CR和LF?编译代码时出错:

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

illegal line end in character literal


推荐答案

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

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

String someString = "foo\u000Abar";

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

It will be compiled exactly as if you wrote:

String someString = "foo
bar";

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

Stick to \r (carriage return; 0x0D) and \n (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: \u000a\u007d\u007b
            System.out.println("false is true!");
        }
    }
}

这篇关于为什么我不能在Java中使用\ u000D和\ u000A作为CR和LF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆