Java注释中的\ u导致错误信息,为什么? [英] \u in Java comment is leading to error message, why?

查看:1094
本文介绍了Java注释中的\ u导致错误信息,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// \u represent unicode sequence
    char c = '\u0045';
    System.out.println(c);

代码只是这么多,eclipse显示以下错误消息

Code is only this much and eclipse is showing following error message

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Invalid unicode

现在,当我从评论中删除 \u 时它工作正常,有什么问题? code> \ u 评论?这是一个错误还是有一些关系,因为认为Java应该保留评论。

Now when I remove \u from comment it is working fine, what is the problem with \u in comment? IS this a bug or there is some relation, as far as think Java should leave the comments as it is.

推荐答案

编译器会首先读取你的整个源代码(稍后会忽略注释),但是他无法识别\ u,因为它不是有效的unicode-character。

The compiler will read your entire source code first (the comment will be ignored later), but he can't recognize "\u ", because it is no valid unicode-character.

要修复它,你可以写

// \\u represent unicode sequence (extra backslash for escaping)

// \ u represent unicode sequence (extra whitespace for escaping)

编辑:

这是因为编译器首先将所有内容翻译成unicode。编写这个简单的程序


This is because the compiler translates everything into unicode first. Writing this simple programm

class Ideone
{
    public static void main (String[] args)
    {
        // \u000A System.out.println("Hi");
    }
}

输出,因为 \ u000A 代表换行符。 证明

outputs Hi, because \u000A stands for newline. Proof

这篇关于Java注释中的\ u导致错误信息,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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