Java Documentation Comments
Java supports three types of comments:
- Single-line comment
- Multi-line comment
- Documentation comment
The first two are // and /* /, and the third is called a documentation comment, which starts with /** and ends with */.
For the content of the first two comment types, refer to:Java Comments
Documentation comments allow you to embed information about the program within the program.
You can use the javadoc tool to generate information and output it to HTML files.
Documentation comments make it more convenient for you to record your program information.
javadoc Tags
The javadoc tool recognizes the following tags:
| tag | Description | Example |
|---|---|---|
| @author | Identifies the author of a class | @author description |
| @deprecated | Indicates a deprecated class or member | @deprecated description |
| {@docRoot} | Specifies the path of the current documentation root directory | Directory Path |
| @exception | Flags an exception thrown by a class | @exception exception-name explanation |
| {@inheritDoc} | Comments inherited from the direct parent class | Inherits a comment from the immediate surperclass. |
| {@link} | Inserts a link to another topic | {@link name text} |
| {@linkplain} | Inserts a link to another topic, but displays the link in plain text font | Inserts an in-line link to another topic. |
| @param | Describes a parameter of a method | @param parameter-name explanation |
| @return | Describes the return value type | @return explanation |
| @see | Specifies a link to another topic | @see anchor |
| @serial | Describes a serializable attribute | @serial description |
| @serialData | Describes data written via the writeObject() and writeExternal() methods | @serialData description |
| @serialField | Describes an ObjectStreamField component | @serialField name type description |
| @since | Marks when a specific change was introduced | @since release |
| @throws | Same as the @exception tag. | The @throws tag has the same meaning as the @exception tag. |
| {@value} | Displays the value of a constant, which must be a static attribute. | Displays the value of a constant, which must be a static field. |
| @version | Specifies the version of the class | @version info |
Documentation comment
At the beginning of /**, the first line or several lines are the main description of the class, variables, and methods.
After that, you can include one or more various @ tags. Each @ tag must be at the beginning of a new line or immediately follow an asterisk * at the beginning of a line.
Multiple tags of the same type should be grouped together. For example, if you have three @see tags, you can place them one after another.
The following is an example of a stable comment for a class:
/*
** This class draws a bar chart *
@author
runoob *
@version
1.2
*/
What javadoc Outputs
The javadoc tool takes the source code of your Java program as input and outputs some HTML files containing your program comments.
The information for each class will be in its own HTML file. javadoc can also output an inherited tree structure and an index.
Because javadoc implementations differ, the behavior may also differ. You need to check the details of your Java development system version and choose an appropriate Javadoc version.
Example
The following is a simple example using documentation comments. Note that each comment appears before the item it describes.
After being processed by javadoc, the comments for the SquareNum class will be found in SquareNum.html.
SquareNum.java file code:
import
java
.
io
.*;
/*
-
- This class demonstrates documentation comments *
@author
Ayan Amhed *
@version
1.2
*/
public
class
SquareNum
{
/*
-
- This method returns the square of num. * This is a multiline description. You can use * as many lines as you like. *
@param
num The value to be squared. *
@return
num squared.
*/
public
double
square
(
double
num
)
{
return
num
num
;
}
/*
-
- This method inputs a number from the user. *
@return
The value input as a double. *
@exception
IOException On input error. *
@see
IOException
*/
public
double
getNumber
(
)
throws
IOException
{
InputStreamReader
isr
=
new
InputStreamReader
(
System
.
in
)
;
BufferedReader
inData
=
new
BufferedReader
(
isr
)
;
String
str
;
str
=
inData
.
readLine
(
)
;
return
(
new
Double
(
str
)
)
.
doubleValue
(
)
;
}
/*
-
- This method demonstrates square(). *
@param
args Unused. *
@return
Nothing. *
@exception
IOException On input error. *
@see
IOException
*/
public
static
void
main
(
String
args
[
]
)
throws
IOException
{
SquareNum
ob
=
new
SquareNum
(
)
;
double
val
;
System
.
out
.
println
(
"
Enter value to be squared:
"
)
;
val
=
ob
.
getNumber
(
)
;
val
=
ob
.
square
(
val
)
;
System
.
out
.
println
(
"
Squared value is
"
val
)
;
}
}
As follows, use the javadoc tool to process the SquareNum.java file:
$ javadoc SquareNum.java
Loading source file SquareNum.java...
Constructing Javadoc information...
Standard Doclet version 1.5.0_13
Building tree for all the packages and classes...
Generating SquareNum.html...
SquareNum.java:39: warning - @return tag cannot be used\
in method with void return type.
Generating package-frame.html...
Generating package-summary.html...
Generating package-tree.html...
Generating constant-values.html...
Building index for all the packages and classes...
Generating overview-tree.html...
Generating index-all.html...
Generating deprecated-list.html...
Building index for all classes...
Generating allclasses-frame.html...
Generating allclasses-noframe.html...
Generating index.html...
Generating help-doc.html...
Generating stylesheet.css...
1 warning
$