简体   繁体   English

Java评论中的/ **和/ *

[英]/** and /* in Java Comments

What's the difference between 有什么区别

/**
 * comment
 *
 *
 */

and

/*
 * 
 * comment
 *
 */

in Java? 在Java? When should I use them? 我应该什么时候使用它们?

The first form is called Javadoc . 第一种形式称为Javadoc You use this when you're writing formal APIs for your code, which are generated by the javadoc tool. 当您为代码编写正式API时,可以使用它,这些API是由javadoc工具生成的。 For an example, the Java 7 API page uses Javadoc and was generated by that tool. 例如, Java 7 API页面使用Javadoc并由该工具生成。

Some common elements you'd see in Javadoc include: 您在Javadoc中看到的一些常见元素包括:

  • @param : this is used to indicate what parameters are being passed to a method, and what value they're expected to have @param :这用于指示传递给方法的参数,以及它们应具有的值

  • @return : this is used to indicate what result the method is going to give back @return :这用于表示该方法将返回的结果

  • @throws : this is used to indicate that a method throws an exception or error in case of certain input @throws :这用于表示方法在某些输入的情况下抛出异常或错误

  • @since : this is used to indicate the earliest Java version this class or function was available in @since :这用于表示此类或函数可用的最早Java版本

As an example, here's Javadoc for the compare method of Integer : 举个例子,这里是Integer compare方法的Javadoc:

/**
 * 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);
}

The second form is a block (multi-line) comment. 第二种形式是块(多行)注释。 You use this if you want to have multiple lines in a comment. 如果要在注释中包含多行,请使用此选项。

I will say that you'd only want to use the latter form sparingly ; 我会说你只想谨慎使用后一种形式; that is, you don't want to overburden your code with block comments that don't describe what behaviors the method/complex function is supposed to have. 也就是说,您不希望使用块注释来使代码过载,这些注释不描述方法/复杂函数应具有的行为。

Since Javadoc is the more descriptive of the two, and you can generate actual documentation as a result of using it, using Javadoc would be more preferable to simple block comments. 由于Javadoc对两者的描述性更强,并且您可以使用它生成实际文档,因此使用Javadoc将比简单块注释更可取。

For the Java programming language , there is no difference between the two. 对于Java 编程语言两者之间没有区别 Java has two types of comments: traditional comments ( /* ... */ ) and end-of-line comments ( // ... ). Java有两种类型的注释:传统注释( /* ... */ )和行尾注释( // ... )。 See the Java Language Specification . 请参阅Java语言规范 So, for the Java programming language, both /* ... */ and /** ... */ are instances of traditional comments, and they are both treated exactly the same by the Java compiler, ie, they are ignored (or more correctly: they are treated as white space). 因此,对于Java编程语言, /* ... *//** ... */都是传统注释的实例,Java编译器对它们的处理方式完全相同,即它们被忽略(或者更正确:它们被视为空白区域)。

However, as a Java programmer, you do not only use a Java compiler. 但是,作为Java程序员,您不仅要使用Java编译器。 You use a an entire tool chain, which includes eg the compiler, an IDE, a build system, etc. And some of these tools interpret things differently than the Java compiler. 您使用整个工具链,其中包括编译器,IDE,构建系统等。其中一些工具的解释方式与Java编译器不同。 In particular, /** ... */ comments are interpreted by the Javadoc tool, which is included in the Java platform and generates documentation. 特别是, /** ... */ comments由Javadoc工具解释,该工具包含在Java 平台中并生成文档。 The Javadoc tool will scan the Java source file and interpret the parts between /** ... */ as documentation. Javadoc工具将扫描Java源文件并将/** ... */之间的部分解释为文档。

This is similar to tags like FIXME and TODO : if you include a comment like // TODO: fix this or // FIXME: do that , most IDEs will highlight such comments so that you don't forget about them. 这类似于FIXMETODO类的标签:如果你包含像// TODO: fix this这样的// TODO: fix this// FIXME: do that ,大多数IDE会突出显示这些注释,这样你就不会忘记它们。 But for Java, they are just comments. 但对于Java来说,它们只是评论。

The first is Javadoc comments. 首先是Javadoc评论。 They can be processed by the javadoc tool to generate the API documentation for your classes. 它们可以由javadoc工具处理,以生成类的API文档。 The second is a normal block comment. 第二个是正常的块评论。

Reading the section 3.7 of JLS well explain all you need to know about comments in Java. 阅读JLS的3.7,可以很好地解释Java中有关注释的所有知识。

There are two kinds of comments: 评论有两种:

  • /* text */ / * text * /

A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++). 传统注释:忽略ASCII字符/ *到ASCII字符* /的所有文本(如C和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++). 行尾注释:忽略从ASCII字符//到行尾的所有文本(如在C ++中)。


About your question, 关于你的问题,

The first one 第一个

/**
 *
 */

is used to declare Javadoc Technology . 用于声明Javadoc技术

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. Javadoc是一个工具,它解析一组源文件中的声明和文档注释,并生成一组描述类,接口,构造函数,方法和字段的HTML页面。 You can use a Javadoc doclet to customize Javadoc output. 您可以使用Javadoc doclet自定义Javadoc输出。 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. doclet是使用Doclet API编写的程序,它指定了工具生成的输出的内容和格式。 You can write a doclet to generate any kind of text file output, such as HTML, SGML, XML, RTF, and MIF. 您可以编写doclet来生成任何类型的文本文件输出,例如HTML,SGML,XML,RTF和MIF。 Oracle provides a standard doclet for generating HTML-format API documentation. Oracle提供了用于生成HTML格式API文档的标准doclet。 Doclets can also be used to perform special tasks not related to producing API documentation. Doclet还可用于执行与生成API文档无关的特殊任务。

For more information on Doclet refer to the API . 有关Doclet更多信息,请参阅API

The second one, as explained clearly in JLS, will ignore all the text between /* and */ thus is used to create multiline comments. 如JLS中清楚解释的那样,第二个将忽略/**/之间的所有文本,因此用于创建多行注释。


Some other things you might want to know about comments in Java 您可能想要了解Java中的注释的其他一些事情

  • Comments do not nest. 评论不嵌套。
  • /* and */ have no special meaning in comments that begin with // . /* and */在以//开头的注释中没有特殊含义。
  • // has no special meaning in comments that begin with /* or /** . //在以/* or /**开头的注释中没有特殊含义。
  • The lexical grammar implies that comments do not occur within character literals ( §3.10.4 ) or string literals ( §3.10.5 ). 词法语法意味着在字符文字( §3.10.4 )或字符串文字( §3.10.5 )中不会出现注释。

Thus, the following text is a single complete comment: 因此,以下文字是一个完整的评论:

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

I don't think the existing answers adequately addressed this part of the question: 我认为现有的答案没有充分解决这部分问题:

When should I use them? 我应该什么时候使用它们?

If you're writing an API that will be published or reused within your organization, you should write comprehensive Javadoc comments for every public class, method, and field, as well as protected methods and fields of non- final classes. 如果您正在编写将在组织内发布或重用的API,则应为每个public类,方法和字段以及非final类的protected方法和字段编写全面的Javadoc注释。 Javadoc should cover everything that cannot be conveyed by the method signature, such as preconditions, postconditions, valid arguments, runtime exceptions, internal calls, etc. Javadoc应该涵盖方法签名无法传达的所有内容,例如前置条件,后置条件,有效参数,运行时异常,内部调用等。

If you're writing an internal API (one that's used by different parts of the same program), Javadoc is arguably less important. 如果你正在编写一个内部API(由同一个程序的不同部分使用的那个),那么Javadoc可能不那么重要了。 But for the benefit of maintenance programmers, you should still write Javadoc for any method or field where the correct usage or meaning is not immediately obvious. 但是为了维护程序员的利益,你仍然应该为正确的用法或含义不是很明显的任何方法或领域编写Javadoc。

The "killer feature" of Javadoc is that it's closely integrated with Eclipse and other IDEs. Javadoc的“杀手级功能”是它与Eclipse和其他IDE紧密集成。 A developer only needs to hover their mouse pointer over an identifier to learn everything they need to know about it. 开发人员只需将鼠标指针悬停在标识符上即可了解他们需要了解的所有内容。 Constantly referring to the documentation becomes second nature for experienced Java developers, which improves the quality of their own code. 不断引用文档成为有经验的Java开发人员的第二天性,这提高了他们自己代码的质量。 If your API isn't documented with Javadoc, experienced developers will not want to use it. 如果您的API没有使用Javadoc记录,经验丰富的开发人员将不希望使用它。

Comments in the following listing of Java Code are the greyed out characters: 以下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.
    }
}

The Java language supports three kinds of comments: Java语言支持三种注释:

/* text */

The compiler ignores everything from /* to */ . 编译器忽略从/**/

/** documentation */

This indicates a documentation comment (doc comment, for short). 这表示文档注释(简称doc注释)。 The compiler ignores this kind of comment, just like it ignores comments that use /* and */ . 编译器会忽略这种注释,就像忽略使用/**/的注释一样。 The JDK javadoc tool uses doc comments when preparing automatically generated documentation. JDK javadoc工具在准备自动生成的文档时使用doc注释。

// text

The compiler ignores everything from // to the end of the line. 编译器会忽略从//到行尾的所有内容。

Now regarding when you should be using them: 现在关于何时应该使用它们:

Use // text when you want to comment a single line of code. 如果要评论单行代码,请使用// text

Use /* text */ when you want to comment multiple lines of code. 如果要注释多行代码,请使用/* text */

Use /** documentation */ when you would want to add some info about the program that can be used for automatic generation of program documentation. 当您想要添加有关可用于自动生成程序文档的程序的一些信息时,请使用/** documentation */

First one is for Javadoc you define on the top of classes, interfaces, methods etc. You can use Javadoc as the name suggest to document your code on what the class does or what method does etc and generate report on it. 第一个用于Javadoc,您可以在类,接口,方法等的顶部进行定义。您可以使用Javadoc作为名称建议来记录您的代码,了解类的功能或方法的作用等,并生成报告。

Second one is code block comment. 第二个是代码块注释。 Say for example you have some code block which you do not want compiler to interpret then you use code block comment. 比如说你有一些代码块,你不希望编译器解释然后你使用代码块注释。

another one is // this you use on statement level to specify what the proceeding lines of codes are supposed to do. 另一个是//这是你在语句级别上使用来指定代码的前进行应该做什么。

There are some other also like //TODO, this will mark that you want to do something later on that place 还有其他一些像// TODO,这标志着你想在以后的那个地方做点什么

//FIXME you can use when you have some temporary solution but you want to visit later and make it better. //当您有一些临时解决方案时可以使用FIXME,但是您想稍后访问并使其更好。

Hope this helps 希望这可以帮助

  • Single comment eg: //comment 单一评论例如://评论
  • Multi Line comment eg: /* comment */ 多行评论例如:/ *评论* /
  • javadoc comment eg: /** comment */ javadoc评论例如:/ **评论* /

Java supports two types of comments: Java支持两种类型的注释:

  • /* multiline comment */ : The compiler ignores everything from /* to */ . /* multiline comment */ :编译器忽略从/**/ The comment can span over multiple lines. 评论可以跨越多行。

  • // single line : The compiler ignores everything from // to the end of the line. // single line :编译器忽略从//到行尾的所有内容。

Some tool such as javadoc use a special multiline comment for their purpose. 某些工具(如javadoc)使用特殊的多行注释来实现其目的。 For example /** doc comment */ is a documentation comment used by javadoc when preparing the automatically generated documentation, but for Java it's a simple multiline comment. 例如/** doc comment */是javadoc在准备自动生成的文档时使用的文档注释,但对于Java来说,这是一个简单的多行注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM