/** 和 /* 在 Java 注释中

/** and /* in Java Comments

有什么区别
/**
 * comment
 *
 *
 */

/*
 * 
 * comment
 *
 */

在Java?我应该什么时候使用它们?

首先是Javadoc注释。它们可以由 javadoc 工具处理,为您的 类 生成 API 文档。第二个是普通的块注释。

第一个表单称为 Javadoc. You use this when you're writing formal APIs for your code, which are generated by the javadoc tool. For an example, the Java 7 API page 使用 Javadoc 并由该工具生成。

您会在 Javadoc 中看到的一些常见元素包括:

  • @param:这用于指示传递给方法的参数是什么,以及它们应该具有什么值

  • @return:这个用来表示这个方法要返回什么结果

  • @throws:用于表示方法在某些输入的情况下抛出异常或错误

  • @since: 这用于指示最早的 Java 版本此 class 或函数在

  • 中可用

例如,JavaIntegercompare 方法的文档:

/**
 * Compares two {@code int} values numerically.
 * The value returned is identical to what would be returned by:
 * <pre>
 *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
 * </pre>
 *
 * @param  x the first {@code int} to compare
 * @param  y the second {@code int} to compare
 * @return the value {@code 0} if {@code x == y};
 *         a value less than {@code 0} if {@code x < y}; and
 *         a value greater than {@code 0} if {@code x > y}
 * @since 1.7
 */
public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

第二种形式是块(多行)注释。如果你想在评论中有多行,你可以使用它。

我会说你只想使用后一种形式有节制地;也就是说,您不想让没有描述 method/complex 函数应该具有的行为的块注释使您的代码负担过重。

由于 Javadoc 是两者中更具描述性的,并且您可以通过使用它生成实际文档,因此使用 Javadoc 比简单的块注释更可取。

Java支持两种评论方式:

  • /* multiline comment */ :编译器忽略从 /**/ 的所有内容。评论可以跨越多行。

  • // single line :编译器忽略从 // 到行尾的所有内容。

某些工具,例如 javadoc 使用特殊的多行注释来实现它们的目的。例如 /** doc comment */ 是 javadoc 在准备自动生成的文档时使用的文档注释,但对于 Java 它是一个简单的多行注释。

以下Java代码清单中的注释为灰色字符:

/** 
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

Java语言支持三种注释:

/* text */

编译器忽略从 /**/ 的所有内容。

/** documentation */

这表示文档注释(简称doc comment)。编译器忽略这种注释,就像它忽略使用 /**/ 的注释一样。 JDK javadoc 工具在准备自动生成的文档时使用文档注释。

// text

编译器忽略从 // 到行尾的所有内容。

关于何时应该使用它们:

当您想注释单行代码时使用// text

当您想注释多行代码时使用/* text */

当您想要添加一些有关可用于自动生成程序文档的程序的信息时,请使用 /** documentation */

第一个是针对您在 classes、接口、方法等之上定义的 Javadoc。您可以使用 Javadoc 顾名思义来记录您的代码,说明 class 的作用或什么方法等等并生成报告。

第二个是代码块注释。 比如说你有一些你不希望编译器解释的代码块然后你使用代码块注释。

另一个是// 您可以在语句级别使用它来指定前面的代码行应该做什么。

还有一些其他的也像//TODO,这将标记你想稍后在那个地方做一些事情

//当你有一些临时解决方案但你想稍后访问并改进它时可以使用FIXME。

希望对您有所帮助

对于Java编程语言,两者没有区别。 Java有两种注释:传统注释(/* ... */)和行尾注释(// ...)。参见Java Language Specification。因此,对于 Java 编程语言,/* ... *//** ... */ 都是传统注释的实例,Java 编译器对它们的处理完全相同,即,它们被忽略(或更准确地说:它们被视为白色 space)。

但是,作为 Java 程序员,您不仅仅使用 Java 编译器。您使用整个工具链,其中包括例如编译器、IDE、构建系统等。其中一些工具对事物的解释与 Java 编译器不同。特别是,/** ... */ 评论由 Javadoc 工具解释,该工具包含在 Java 平台 中并生成文档。 Javadoc 工具将扫描 Java 源文件并将 /** ... */ 之间的部分解释为文档。

这类似于 FIXMETODO 等标签:如果您包含 // TODO: fix this// FIXME: do that 等评论,大多数 IDE 将突出显示这样的评论,这样你就不会忘记它们。但是对于 Java,它们只是注释。

阅读 3.7 of JLS 部分很好地解释了您需要了解的关于 Java 中评论的所有信息。

There are two kinds of comments:

  • /* text */

A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++).

  • //text

An end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored (as in C++).


关于你的问题,

第一个

/**
 *
 */

用于声明Javadoc Technology

Javadoc is a tool that parses the declarations and documentation comments in a set of source files and produces a set of HTML pages describing the classes, interfaces, constructors, methods, and fields. You can use a Javadoc doclet to customize Javadoc output. A doclet is a program written with the Doclet API that specifies the content and format of the output to be generated by the tool. You can write a doclet to generate any kind of text file output, such as HTML, SGML, XML, RTF, and MIF. Oracle provides a standard doclet for generating HTML-format API documentation. Doclets can also be used to perform special tasks not related to producing API documentation.

有关 Doclet 的更多信息,请参阅 API

第二个,正如 JLS 中清楚解释的那样,将忽略 /**/ 之间的所有文本,因此用于创建多行注释。


关于 Java

中的评论,您可能还想了解其他一些事情
  • 评论不嵌套。
  • /* and */在以//.
  • 开头的注释中没有特殊意义
  • //在以/* or /**开头的注释中没有特殊意义。
  • 词法语法意味着注释不会出现在字符文字中 (§3.10.4) or string literals (§3.10.5)。

因此,以下文字是一个完整的评论:

/* this comment /* // /** ends here: */

我认为现有的答案没有充分解决这部分问题:

When should I use them?

如果您正在编写将在您的组织内发布或重复使用的 API,您应该为每个 public class 编写全面的 Java 文档注释,方法和字段,以及 protectedfinal class 方法和字段。 Javadoc 应涵盖方法签名不能传达的所有内容,例如前置条件、后置条件、有效参数、运行时异常、内部调用等

如果您正在编写内部 API(由同一程序的不同部分使用的内部文档),则 Javadoc 可能不太重要。但是为了维护程序员的利益,您仍然应该为正确用法或含义不是很明显的任何方法或字段编写 Javadoc。

Javadoc的"killer feature"在于它与Eclipse等IDE紧密结合。开发人员只需将鼠标指针悬停在标识符上即可了解他们需要了解的一切。不断参考文档成为经验丰富的 Java 开发人员的第二天性,这提高了他们自己代码的质量。如果您的 API 没有记录在 Javadoc 中,有经验的开发人员将不想使用它。

  • 单个评论例如://comment
  • 多行注释例如:/*注释*/
  • javadoc 注释例如:/** 注释 */