What's the difference between
/**
* comment
*
*
*/
and
/*
*
* comment
*
*/
in Java? When should I use them?
What's the difference between
/**
* comment
*
*
*/
and
/*
*
* comment
*
*/
in Java? When should I use them?
I don't think the existing answers adequately addressed this part of the question:
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 asprotected
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.
For the Java programming language, there is no difference between the two. Java has two types of comments: traditional comments (
/* ... */
) and end-of-line comments (// ...
). See the Java Language Specification. 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, i.e., they are ignored (or more correctly: they are treated as white space).However, as a Java programmer, you do not only use a Java compiler. You use a an entire tool chain, which includes e.g. the compiler, an IDE, a build system, etc. And some of these tools interpret things differently than the Java compiler. In particular,
/** ... */
comments are interpreted by the Javadoc tool, which is included in the Java platform and generates documentation. The Javadoc tool will scan the Java source file and interpret the parts between/** ... */
as documentation.This is similar to tags like
FIXME
andTODO
: 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. But for Java, they are just comments.Comments in the following listing of Java Code are the greyed out characters:
The Java language supports three kinds of comments:
The compiler ignores everything from
/*
to*/
.This indicates a documentation comment (doc comment, for short). 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.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.Use
/* text */
when you want to comment multiple lines of code.Use
/** documentation */
when you would want to add some info about the program that can be used for automatic generation of program documentation.The first form is called 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 uses Javadoc and was generated by that tool.Some common elements you'd see in Javadoc include:
@param
: this is used to indicate what parameters are being passed to a method, and what value they're expected to have@return
: this is used to indicate what result the method is going to give back@throws
: this is used to indicate that a method throws an exception or error in case of certain input@since
: this is used to indicate the earliest Java version this class or function was available inAs an example, here's Javadoc for the
compare
method ofInteger
: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.
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.
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
//FIXME you can use when you have some temporary solution but you want to visit later and make it better.
Hope this helps