gpt4 book ai didi

java - 为什么 java 编译器在实际编译之前剥离所有 unicode 字符?

转载 作者:行者123 更新时间:2023-11-30 11:11:38 29 4
gpt4 key购买 nike

<分区>

我是 Java 的新手,我有这样的代码:

    public class Puzzle {
public static void main(String... args) {
System.out.println("Hi Guys!");
// Character myChar = new Character('\u000d');
}
}

可以看到一行:

Character myChar = new Character('\u000d');

被注释掉了。但是,当我运行 javac 时,仍然会出现这样的错误:

Puzzle.java:9: error: unclosed character literal
// Character myChar = new Character('\u000d');
^
1 error

在此blog发布我找到异常的原因。博客说:

Java compiler, just before the actual compilation strips out all the unicode characters and coverts it to character form. This parsing is done for the complete source code which includes the comments also. After this conversion happens then the Java compilation process continues.

In our code, the when Java compiler encounters \u000d, it considers this as a newline and changes the code as below,

public class Puzzle {
public static void main(String... args) {
System.out.println("Hi Guys!");
// Character myChar = new Character('
');
}
}

有了这个我有两个问题:

  1. 为什么 Java 首先解析 unicode?它有什么优点吗?
  2. 因为该行仍然被注释,所以 Java 正在尝试解析它!这是唯一的情况吗?或者它通常也解析注释行吗?我很困惑。

提前致谢。

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