DOCUMENTATION IN JAVA17
DOCUMENTATION IN JAVA17
Java Comments
The Java comments are the statements in a program that are not executed by the compiler and
interpreter. In a program, comments are like indents one makes, they are used so that it is easier for
someone who is not familiar with the language to be able to understand the code. It will also make
the job easier for you, as a coder, to find errors in the code since you will be easily able to find the
location of the bug. Comments are ignored by the compiler while compiling a code, which makes the
job more complex in the long run when they have to go through so much code to find one line.
The single-line comment is used to comment only one line of the code. It is the widely used and easiest
way of commenting the statements.
Single line comments starts with two forward slashes (//). Any text in front of // is not executed by
Java.
Syntax:
CommentExample1.java
Output:
10
The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex
code snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line
comments there).
Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by
Java.
Syntax:
/*
This
is
multi line
comment
*/
Output:
10
Documentation comments are usually used to write large programs for a project or software
application as it helps to create documentation API. These APIs are needed for reference, i.e., which
classes, methods, arguments, etc., are used in the code.
To create documentation API, we need to use the javadoc tool. The documentation comments are
placed between /** and */.
Syntax:
/**
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags
*
*/
Creating API Document | javadoc tool
We can create document api in java by the help of javadoc tool. In the java file, we must use the
documentation comment /**... */ to post information for the class, method, constructor, fields etc.
package com.abc;
/** This class is a user-defined class that contains one methods cube.*/
public class M{
To create the document API, you need to use the javadoc tool followed by java file name. There is no
need to compile the javafile.
javadoc M.java
to generate the document api. Now, there will be created a lot of html files. Open the index.html file
to get the information about the classes.
javadoc tags
@code {@code text} To show the text in code font without interpreting it as html
markup or nested javadoc tag.
@param @param parameter-name To add a parameter with given name and description to
description 'Parameters' section.
@return @return description Required for every method that returns something (except
void)
/**
* <h1>Find average of three numbers!</h1>
* The FindAvg program implements an application that
* simply calculates average of three integers and Prints
* the output on the screen.
*
* @author Pratik Agarwal
* @version 1.0
* @since 2017-02-18
*/
public class FindAvg
{
/**
* This method is used to find average of three integers.
* @param numA This is the first parameter to findAvg method
* @param numB This is the second parameter to findAvg method
* @param numC This is the second parameter to findAvg method
* @return int This returns average of numA, numB and numC.
*/
public int findAvg(int numA, int numB, int numC)
{
return (numA + numB + numC)/3;
}
/**
* This is the main method which makes use of findAvg method.
Output:
A doc comment is written in HTML and must precede a class, field, constructor or method
declaration. It is made up of two parts -- a description followed by block tags. In this example, the
block tags are @param, @return, and @see.
/**
* Returns an Image object that can then be painted on the screen.
* The url argument must specify an absolute <a href="#{@link}">{@link URL}</a>. The name
* argument is a specifier that is relative to the url argument.
* <p>
* This method always returns immediately, whether or not the
* image exists. When this applet attempts to draw the image on
* the screen, the data will be loaded. The graphics primitives
* that draw the image will incrementally paint on the screen.
*
* @param url an absolute URL giving the base location of the image
* @param name the location of the image, relative to the url argument
* @return the image at the specified URL
* @see Image
*/
Points to Note;
• The first line contains the begin-comment delimiter ( /**).
• Starting with Javadoc 1.4, the leading asterisks are optional.
• Write the first sentence as a short summary of the method, as Javadoc automatically places
it in the method summary table (and index).
• Notice the inline tag {@link URL}, which converts to an HTML hyperlink pointing to the
documentation for the URL class. This inline tag can be used anywhere that a comment can
be written, such as in the text following block tags.
• If you have more than one paragraph in the doc comment, separate the paragraphs with
a <p> paragraph tag, as shown.
• Insert a blank comment line between the description and the list of tags, as shown.
• The first line that begins with an "@" character ends the description. There is only one
description block per doc comment; you cannot continue the description following block
tags.
• The last line contains the end-comment delimiter ( */) Note that unlike the begin-comment
delimiter, the end-comment contains only a single asterisk.