Java如何进行词法翻译?
How Java execute the lexical translation?
在 Jave 规范中,我读到了
A translation of Unicode escapes (§3.3) in the raw stream of Unicode characters to the corresponding Unicode character. A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.here
意思是词法翻译只适用于ASCII字符?因为当我尝试用西里尔字母、希伯来字母或汉字字符编写代码时,即使这些字符不是 ASCII,也没有编译时错误?
我不明白为什么?谁能帮我理解
引用并没有说明如果您编写包含 Cyrillic/Hebrew 字母的程序会发生什么。事实上,你引用的那一节之前说:
3.1 Unicode
Programs are written using the Unicode character set.
请注意,此处的“允许”表示此转换步骤为 Java 添加了一项新功能。当你被允许做某事时,你可以,但不需要去做。
引用只是说词法翻译器会将 \uxxxx
形式的任何内容转换为相应的 Unicode 字符 U+xxxx。
这样做的自然结果是,您可以仅使用 ASCII 键盘编写包含任何 Unicode 代码点的程序(即“任何程序”)。如何?每当你需要写一些非 ASCII 字符时,只需写下它的 Unicode 转义字符。
举个具体的例子:
这些是有效的 Java 陈述:
int Д = 0;
System.out.println("Д");
但是假设我的文本编辑器只能处理 ASCII 文本,或者我只有美式键盘,所以我不能输入“Д”。语言规范说我仍然可以用 ASCII 写这个,像这样:
int \u0414 = 0;
System.out.println("\u0414");
它会做同样的事情。
在 Jave 规范中,我读到了
A translation of Unicode escapes (§3.3) in the raw stream of Unicode characters to the corresponding Unicode character. A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.here
意思是词法翻译只适用于ASCII字符?因为当我尝试用西里尔字母、希伯来字母或汉字字符编写代码时,即使这些字符不是 ASCII,也没有编译时错误?
我不明白为什么?谁能帮我理解
引用并没有说明如果您编写包含 Cyrillic/Hebrew 字母的程序会发生什么。事实上,你引用的那一节之前说:
3.1 Unicode
Programs are written using the Unicode character set.
请注意,此处的“允许”表示此转换步骤为 Java 添加了一项新功能。当你被允许做某事时,你可以,但不需要去做。
引用只是说词法翻译器会将 \uxxxx
形式的任何内容转换为相应的 Unicode 字符 U+xxxx。
这样做的自然结果是,您可以仅使用 ASCII 键盘编写包含任何 Unicode 代码点的程序(即“任何程序”)。如何?每当你需要写一些非 ASCII 字符时,只需写下它的 Unicode 转义字符。
举个具体的例子:
这些是有效的 Java 陈述:
int Д = 0;
System.out.println("Д");
但是假设我的文本编辑器只能处理 ASCII 文本,或者我只有美式键盘,所以我不能输入“Д”。语言规范说我仍然可以用 ASCII 写这个,像这样:
int \u0414 = 0;
System.out.println("\u0414");
它会做同样的事情。