gpt4 book ai didi

Java Unicode 翻译

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:50:36 26 4
gpt4 key购买 nike

我遇到了以下代码:

public class LinePrinter {
public static void main(String args[]) {
//Note: \u000A is unicode for Line Feed
char c=0x000A;
System.out.println(c);
}
}

由于已完成 Unicode 替换,因此无法编译。

问题是,为什么注释 (//) 不覆盖编译器完成的 Unicode 替换?我认为编译器应该先忽略注释,然后再对代码进行翻译。

编辑:

不知道上面说的够不够清楚。

我知道上面会发生什么以及为什么会出错。我的期望是编译器在对代码进行任何翻译之前应该忽略所有注释行。显然,这里不是这种情况。我期待这种行为的理由。

最佳答案

它在 Java Puzzlers #14 - 解释的摘录中:

The key to understanding this puzzle is that Java provides no special treatment for Unicode escapes within string literals. The compiler translates Unicode escapes into the characters they represent before it parses the program into tokens, such as strings literals [JLS 3.2].

JLS v7 中的相关段落是 paragraph 3.3 :

A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its input, translating the ASCII characters \u followed by four hexadecimal digits to the UTF-16 code unit (§3.1) of the indicated hexadecimal value, and passing all other characters unchanged.

JLS 第 3 节的介绍给出了为什么会出现这种情况的提示:

Programs are written in Unicode (§3.1), but lexical translations are provided (§3.2) so that Unicode escapes (§3.3) can be used to include any Unicode character using only ASCII characters.

关于Java Unicode 翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13761848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com