Executable Comments in Java – Java 中的可执行注释

最后修改: 2023年 11月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Comments can be useful when we need additional notes in our code. They can help us make our code more understandable. Additionally, they can be especially useful in methods that perform complex operations.

当我们需要在代码中添加注释时,注释会非常有用。它们可以帮助我们使代码更易于理解。此外,在执行复杂操作的方法中,注释尤其有用。

In this tutorial, we’ll explore cases where comments in our code can become executable. Or at least it may appear like they can.

在本教程中,我们将探讨代码中的注释可以成为可执行代码的情况。或者至少看起来是这样。

2. Comments

2.意见

Before we dive in, let’s revisit comments in Java. They are part of the Java syntax and come in two basic formats:

在深入了解之前,让我们重温一下 Java 中的 评论。它们是 Java 语法的一部分,有两种基本格式:

  • Single-line comments
  • Multiline comments

The text from the “//” characters to the end of the line represents a single-line comment:

从”//”字符到行尾的文本表示单行注释:

// This is a single-line comment.

Additionally, a multiple-line comment (also known as a multiline comment) starts with the “/*” and ends with the “*/” symbol. Everything in between is treated as a comment:

此外,多行注释(也称为多行注释)以”/*”符号开始,以 “*/”符号结束。中间的所有内容都被视为注释:

/* This is a
 * multiline
 * comment.
 */

3. Comments and Unicode

3.注释和统一码

Now, let’s start with an example. The following code prints “Baeldung” in the standard output:

现在,让我们从一个例子开始。下面的代码在标准输出中打印”Baeldung“:

// \u000d System.out.println("Baeldung");

Because the line begins with the “//”, which represents the start of a single-line comment, we might conclude the “System.out.println(“Baeldung”);” statement is part of that comment as well.

由于该行以”//”开头,而”//”代表单行注释的开头,因此我们可以断定”System.out.println(“Baeldung”);“语句也是该注释的一部分。

However, this isn’t accurate. It’s important to note Java doesn’t allow comment execution.

然而,这并不准确。需要注意的是,Java 不允许执行注释

With that in mind, let’s examine our example in detail and see the reasons why the code prints “Baeldung” in the console.

有鉴于此,让我们详细研究一下我们的示例,看看代码在控制台中打印”Baeldung“的原因。

3.1. Unicode Escapes

3.1 统一编码转义

The code from the example isn’t treated as a comment because of the “\u000d” Unicode escape sequence we placed before it.

示例中的代码没有被视为注释,因为我们在它前面添加了”\u000d“Unicode 转义序列。

All Java programs use the ASCII character set. However, due to the non-Latin characters, we can’t represent using ASCII codes, Java allows Unicode to appear in comments, identifiers, keywords, literals, and separators.

所有 Java 程序都使用 ASCII 字符集。但是,由于我们无法使用 ASCII 编码来表示非拉丁字符,Java 允许 Unicode 出现在注释、标识符、关键字、字面量和分隔符中。

Furthermore, to be able to use all non-ASCII characters in our code, we need to embed them through Unicode escape sequences. They start with a backslash (“\”) followed by the letter “u” which is then followed by a four-digit hexadecimal code of a specific character.

此外,要在代码中使用所有非 ASCII 字符,我们需要通过 Unicode 转义序列来嵌入它们。它们以反斜杠(”\”)开头,后面是字母 “u”,然后是特定字符的四位十六进制代码。

Using this convention, the CR (or Carriage return) becomes “\u000d“.

使用这一约定,CR(或回车键)就变成了”\u000d“。

Additionally, the Unicode escape sequences are transformed into ASCII code using the lexical translation defined in the Java Language Specification.

此外,Unicode 转义序列会使用 Java语言规范中定义的词法翻译转换为 ASCII 编码。

Moving forward, let’s take a closer look at how Java performs the lexical transformation.

接下来,让我们仔细看看 Java 是如何进行词法转换的。

3.2. Lexical Translation

3.2.词汇翻译

When executing the lexical translation, the Unicode encoding takes precedence over any other encoding, even if it’s part of the comment. To put it differently, Java will first encode all Unicode escape sequences and then move forward with other translations.

执行词法翻译时,Unicode 编码优先于任何其他编码,即使它是注释的一部分。换句话说,Java 将首先对所有 Unicode 转义序列进行编码,然后再进行其他翻译。

Simply put, during the transformation, the Unicode escape is translated into the Unicode character. Then, the result of the previous step is translated into the ASCII code.

简单地说,在转换过程中,Unicode 转义字被转换成 Unicode 字符。然后,将上一步的结果转换为 ASCII 码。

As a side effect, our code won’t compile if we put an invalid Unicode escape inside the comment. Java treats everything that starts with the “\u” as a Unicode escape.

另外,如果我们在注释中使用无效的 Unicode 转义符,代码将无法编译。Java 将所有以”\u“开头的字符都视为 Unicode 转义符。

Thanks to this transformation, we can use Unicode escapes to include any Unicode characters using only ASCII characters. This way, ASCII-based programs and tools can still process the code written in Unicode.

有了这种转换,我们就可以使用 Unicode 转义符,只用 ASCII 字符来包含任何 Unicode 字符。这样,基于 ASCII 的程序和工具仍然可以处理以 Unicode 编写的代码。

Now, back to our example. We used the Unicode escape sequence “\u000d“, which represents a new line.

现在,回到我们的示例。我们使用了 Unicode 转义序列”\u000d“,它表示换行。

When we compile our code, the lexical translation will happen first. Therefore, the “\u000d” will translate to the new line. Since, by definition, a single-line comment ends at the end of the line, the code we put after the Unicode escape won’t be part of the comment anymore.

当我们编译代码时,将首先进行词法翻译。因此,”\u000d“将被翻译到新行。根据定义,单行注释在行尾结束,因此,我们在 Unicode 转义后添加的代码将不再是注释的一部分。

As a result of the transformation, our code will appear in the new line:

转换后,我们的代码将出现在新行中:

//
System.out.println("Baeldung");

3.3. Unicode and IDEs

3.3.统一码和集成开发环境

Nowadays, we often use an IDE as a development tool. Additionally, we frequently rely on it and expect it’ll warn us if something in our code seems suspicious.

如今,我们经常使用集成开发环境作为开发工具。此外,我们还经常依赖它,希望它能在我们的代码中出现可疑之处时发出警告。

However, when it comes to IDEs and Unicode characters, depending on the IDE we’re using, it sometimes displays the code in the wrong way. It might not interpret Unicode escape sequences correctly and, thus, displays incorrect code highlighting.

然而,当涉及到集成开发环境和 Unicode 字符时,我们使用的集成开发环境有时会以错误的方式显示代码。它可能无法正确理解 Unicode 转义序列,从而显示错误的代码高亮。

Since we can use Unicode escapes instead of ASCII characters, nothing prevents us from substituting other parts of the code with Unicode escapes:

既然我们可以使用 Unicode 转义字符代替 ASCII 字符,就没有什么可以阻止我们用 Unicode 转义字符代替代码的其他部分:

\u002f\u002f This is a comment
\u0053ystem.out.println("Baeldung");

Here, we replaced the “//” and the letter “S” with Unicode escapes. The code still prints “Baeldung” in the console.

在这里,我们用 Unicode 转义词替换了”//”和字母 “S”。代码仍然在控制台中打印”Baeldung“。

4. Conclusion

4.结论

In this tutorial, we learned how comments and Unicode escape sequences work together.

在本教程中,我们学习了注释和 Unicode 转义序列如何协同工作。

To sum up, Java doesn’t allow executable comments. When using Unicode escapes in our code, Java translates them to ASCII before any other transformation.

总而言之,Java 不允许执行注释。在我们的代码中使用 Unicode 转义符时,Java 会先将其转换为 ASCII,然后再进行其他转换。

Being able to write Unicode characters is useful when we’d like to use non-Latin characters we can’t represent in any other way in our program. Although it’s perfectly legal to write an entire codebase using just Unicode escapes, we should avoid them and use them only when necessary.

当我们想在程序中使用无法以其他方式表示的非拉丁字符时,能够编写 Unicode 字符就非常有用。虽然只使用 Unicode 转义字符编写整个代码库是完全合法的,但我们应该避免使用它们,只有在必要时才使用。