What\'s the difference between
/**
* comment
*
*
*/
and
/*
*
* comment
*
*/
in Java? When shou
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. Javadoc should cover everything that cannot be conveyed by the method signature, such as preconditions, postconditions, valid arguments, runtime exceptions, internal calls, etc.
If you're writing an internal API (one that's used by different parts of the same program), Javadoc is arguably less important. 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.
The "killer feature" of Javadoc is that it's closely integrated with Eclipse and other IDEs. 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. If your API isn't documented with Javadoc, experienced developers will not want to use it.
Java supports two types of comments:
/* multiline comment */
: The compiler ignores everything from /*
to */
. The comment can span over multiple lines.
// single line
: The compiler ignores everything from //
to the end of the line.
Some tool such as javadoc use a special multiline comment for their purpose. 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.
Reading the section 3.7 of JLS well explain all you need to know about comments in 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++).
About your question,
The first one
/**
*
*/
is used to declare 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.
For more information on Doclet
refer to the API.
The second one, as explained clearly in JLS, will ignore all the text between /*
and */
thus is used to create multiline comments.
Some other things you might want to know about comments in Java
/* and */
have no special meaning in comments that begin with //
.//
has no special meaning in comments that begin with /* or /**
.Thus, the following text is a single complete comment:
/* this comment /* // /** ends here: */