There are three main types of annotations in Java: line comments, paragraph comments, document comments
1) Line Comment: The line comment becomes a single-line comment, and the line comment uses the format "//Note text" to annotate or explain the code for a line
public class Linecomment
{
This is an example of a single-line comment
public static void Main (String args[])
{
This is just an example of a single-line comment
SYSTEM.OUT.PRINTLN ("single line Comment");
}
}
The text inside//behind the above code is an example of a line comment
2) Paragraph Note: The paragraph comment is also a multiline comment, usually a comment method when the descriptive text is longer
public class Multicommont
{
/*
* This is a simple example of a paragraph note
* Here is the function entry Main method
*/
public static void Main (String args[])
{
System.out.println ("Multi Lines Comments");
}
}
3) Document Comment: The document comment is a more powerful feature in Java, it can be used for annotation class, properties, methods and other instructions, and through the JDK tool Javadoc directly generated related documents, the basic format of document comments is "/**...*/", not only this, the document comment itself has syntax
[1] Formatting of document and document annotations:
The generated documents are in HTML format, and these HTML-formatted identifiers are not Javadoc, but we write them when we write the comments. Therefore, when formatting the document, you need to include HTML tags appropriately, for example:
/**
*this is first line.<br/>
*this is second line.<br/>
*this is third line.<br/>
**/
[2] Three parts of the documentation note:
The document comments can be divided into three sections based on the effect shown in the document, for example:
/**
A brief description of the *testdoc method
Detailed description of the *<p>testdoc method </p>
* @param testinput string to print input strings
* @return No return value
**/
public void Testdoc (String testinput)
{
System.out.println (Testinput);
}
Summary: In the document, for both the property and the method are existing lists, and then in the following one a detailed description, the list of property names or method names after the description of the paragraph is a summary.
The Department of the property or method of the detailed description, in the format does not need special requirements, can be written in front of the HTML format, as above the second line of content. "*: Here is a technique that is brief and detailed to try not to repeat, the content that appears in the summary does not need to be described in detail for the second time, can be understood as a detailed description is a summary of an extension. The concept of the back section of the code, most of which I did not write comments, but also please readers forgive me! 】
Special note: Except for the two parts above, the last part is the special Description section, the Special Description section uses the Javadoc tag, these are the special marks that the Javadoc tool can parse, which is discussed in the following chapters.
[3] using the Javadoc tag:
Javadoc tags are special tags in Java-inserted document annotations that identify special references in your code. The Javadoc tag consists of the "@" followed by the tag type and the private comment reference. Its format consists of three parts:
@, tag type, private comment reference
Sometimes we can also directly understand the two aspects: @ and the tag type, the special annotation reference; the Javadoc tool resolves special tags embedded in Java document annotations that can help automatically generate a complete formatting API from source code, with tags beginning with the "@" symbol, case-sensitive, Must be entered according to the correct uppercase and lowercase letters. The tag must start at the beginning of a line, otherwise it will be treated as plain text, and the same name tags should be placed together, such as all @see tags should be put together, and then take a look at the meaning of each marker.
@author (1.0): syntax [@author Name-text]
When using the-author option, add a "Author" entry with the specified Name-text when the document is generated, the document comment can contain multiple @author tags, you can specify one or more names for each @author.
@deprecated (1.0): syntax [@deprecated Deprecated-text]
To add a comment, but instead of using the API (although it is available), the Javadoc tool moves Deprecated-text to the front of the description, appears in italics, and adds a bold warning in front of it: "Discourage use." Deprecated-text's first sentence should at least tell the user when to start to discourage the use of the API and what to replace it with. Javadoc only copies the first sentence to the overview section and the index, and the following statement also explains why it is discouraged and should also include a {@link} tag "1.2 version usage" that points to the alternate API
For Javadoc 1.2, use the {@link} tag, which creates inline links where needed, such as:
/**
* @deprecated in JDK x.x, replaced by {@link #methodName (paramlist)}
**/
"*: In the upper Mark x.x refers to the JDK version, the link behind the link is the method signature, MethodName for the method name, paramlist for the parameter list"
For Javadoc 1.1, the standard format is to create a @see tag for each @deprecated tag (it is not inline)
@exception (1.0): syntax [@exception class-name description]
@throws (1.2): syntax [@throws class-name description]
The above two tokens are synonyms, using Class-name and description text to add "throw" subheadings to the generated document, where Class-name is the exception name that the method can throw.
{@link} (1.2): syntax [{@link name label}]
Access to an inline link to the specified name in which the syntax for name and label is exactly the same as the @see tag, as described below, but the resulting inline link is not prevented from being linked in the "See" section. The tag starts with curly braces and ends with curly braces to make it different from the other inline text, and if you need to use "}" in the tag,:& #125 using the HTML entity notation directly;
@param (1.0): syntax [@param parameter-name description]
Add parameters to the "See" section, describing the ability to proceed to the next line, mainly by providing some format of the parameters and describing
@return (1.0): syntax [@return description]
Use description to add the "Return" section, which should describe the return type and allowable range of values
@see (1.0): syntax [@see reference]
The tag is a relatively complex tag that adds a "see" heading with links to reference or text items that can contain any number of @see tags grouped under the same heading, @see in three formats:
@see "string" Note: This form is not used in JDK 1.2, it does not print reference text
Adds a text item to string, does not produce a link, a string is a book or other information reference that is not available through the URL, and Javadoc distinguishes it from the situation by finding the first character as a double quotation mark ("), such as:
@see "This is the Java textbook, mainly for beginners"
The comments above will be generated:
See:
"This is the Java textbook, mainly for beginners"
@see <a href= "page.html#section" >java a section </a>
Add a link to the Url#value definition, where url#value is a relative URL or an absolute url,javadoc tool to differentiate it from other situations by finding the first character lowercase symbol (<), for example:
@see <a href= "page.html#section" > Test Specification </a>
The comments above will be generated:
See:
Test specification
@see package.class#member Label "A more commonly used format"
Adds a link to a label with visible text that points to a document of the specified name in the Java language. Where label is optional, if omitted, the name appears as visible text and is embedded in the <code>html tag, which can be used when you want to abbreviate visible text or visible text that differs from the name.
--package.class#member is any valid name in the Java language--the package name, class name, interface name, constructor name, method name, or domain name--in addition to the dot preceding the member name with the hash character (#), if the name is in the class with the document, Then Javadoc will automatically create a link to it, and to create an external reference link, use the-link option, using either of the other two @see forms to reference a document that does not belong to the name of the referenced class.
--label is optional text, which is a visible label for the link, the label can contain blanks, and if the label is omitted, the Package.class.member is displayed and is shortened relative to the current class and package
--The space is the delimiter between the Package.class#member and the label, and spaces within the parentheses do not represent the beginning of the label, so spaces can be used between the methods ' arguments
Typical forms of @see package.class#member
Referencing members of the current class
@see #field
@see #method (Type,type,...)
@see #method (Type argname,type argname,...)
Referencing the current package or other classes in the import package
@see Class#field
@see Class#method (Type,type,...)
@see Class#method (Type argname,type argname,...)
@see Class
referencing other packages (fully qualified)
@see package. Class#field
@see package. Class#method (Type,type,...)
@see package. Class#method (Type argname,type argname,...)
@see package. Class
A brief description of the @see package:
--the first set of forms (without classes and packages) will cause Javadoc to search only the current class hierarchy. It looks for the current class or interface, its parent class, or a hyper-interface, or a member of its containing class or interface. It does not search for the remainder of the current package or other packages (search step 4-5).
--if any method or constructor is entered as a name without parentheses, such as GetValue, and if there is no domain with the same name, Javadoc will correctly create a link to it, but a warning message will be displayed, prompting for parentheses and arguments to be added. If the method is overloaded, then Javadoc links to the first unspecified method it searches for.
-For all forms, the inner class must be specified as Outer.Inner, rather than as a simple inner.
--as described above, the hash character (#) instead of the dot (.) is used to separate classes and members. This enables Javadoc to parse correctly, because points are also used to separate classes, inner classes, packages, and child packages. When a hash character (#) is the first character, it is absolutely indispensable. However, in other cases, the Javadoc is usually not strict and allows the use of the dot number when there is no ambiguity, but it will display a warning.
The search order for the @see tag--javadoc will process the @see tag that appears in the source file (. java), package file (package.html), or overview file (overview.html), and in the latter two files, you must fully qualify the name provided by @see. In the source file, you can specify a fully qualified or partially qualified name, @see search order:
Current class or interface
Any containing classes and interfaces, first search the nearest
Any parent class and hyper-interface, first search the nearest
Current Package
Any imported packages, classes, and interfaces, searched in the order of the import statements
@since (1.1): syntax [@since Since-text]
Adds a "Since" title to the generated document with the content specified by Since-text, which does not have a special internal structure, which indicates that the change or function has existed since the version of the software specified by Since-text, for example: @since JDK 1.4
@serial (1.2): syntax [@serial field-description]
In the document annotations for the default serializable domain, the optional field-description enhances the ability to describe the document annotation to the field, which must explain the meaning of the field and the acceptable value, if there is a need to describe the number of lines that can be You should add @since tags to each serializable field that you added after the original version of the Serializable class.
@serialField (1.2): syntax [@serialField field-name field-type Field-description]
CV serializable the documentation for the Objectstreamfield component of the Serialpersistentfields member of the class, you should use a @serialfield tag for each Objectstreamfield
@serialData (1.2): syntax [@serialData data-description]
Data-description a document that establishes the sequence and type of data (especially the optional data written by the WriteObject method and all data written by the Externalizable.writeexternal method), @ Serialdata tags can be used in document comments for WriteObject, ReadObject, Writeexternal, and Readexternal methods
@version (1.0): syntax [@version version-text]
When you use the-version option to add a "version" subtitle to the generated document with the content specified by Version-text, the text does not have a special internal structure, and the document comment can contain a maximum of one @version tag.
{@code} (1.5): syntax [{@code text}]
The label is equivalent to the <code>{@literal}</code>, which can be directly filtered out of the HTML tags, you can not < and > to display (< and >), in the text section of this code block , the code can be written directly, even if the use of <B>HELLO</B>, in the HTML will not be recognized as bold Hello, but also the original code snippet <b>Hello</b> format output
{@docRoot} (1.3): syntax [{@docRoot}]
Represents the relative path of the generated file (target) from any generated web directory, when you want to include a copyright page or company logo file when it is useful, you want to reference the generated pages, links from each page at the bottom of the copyright page is common. (@docRoot marks can be used on the command line, and in two document comments: This tag is valid in all document annotations: overview, wrapper classes, interfaces, constructs, methods, and realms, including any part of the tagged text (such as @return, @param, and @deprecated use).
Like what:
/**
* See the <a href= "{@docRoot}/copyright.html" >COPYRIGHT</A>.
**/
{@inheritDoc} (1.4): syntax [{@inheritDoc}]
Inherit (copy) the document from the most recent "inheriting class or executable interface" to the current document comment content in the location of this tag, which allows you to write more general documents related to inheritance
The following situation is more appropriate:
In the main description block of a method, in this case, the main description is a copy of the class and interface inside the entire inheritance tree structure.
Methods such as @return @param and @throws are returned in the text parameter, in which case the text marker is a copy of the label inside the entire hierarchy.
{@linkplain} (1.4): syntax [{@linkplain package.class#member label}]
Like {@link}, unless a linked label is displayed in plain text format, the label can work when the text is normal text, for example:
Refer to {@linkplain Add () The overridden method}
This will be shown as:
Refer to the overridden method
{@value} (1.4): syntax [{@value Package.class#field}]
Used primarily to display the value of a static field:
/**
* The value of this constant is {@value}
**/
public static final String Script_start = "SCRIPT";
[4] Examples of Javadoc Tags:
--[$] An example of using the Javadoc tag--
/**
* @author Lang Yu
* @version 1.2
*/
public class Javadocbasic {
/**
* @see "Main Function JavaDoc"
* @since JDK 1.4
* @param args The params of the console
**/
public static void Main (String args[]) {
System.out.println ("Hello world!");
}
}
For example there is such a small piece of code, on my machine I placed under the D:\Source\work, and then into the directory, using the following command:
d:\source\work>javadoc-d Doc Javadocbasic.java
Using this command, after executing the command, the computer has the following output, and go to the bottom of the directory can see a doc folder, the browser opens inside the index.html can see the content of the generated document:
Loading source file Javadocbasic.java ...
Constructing Javadoc information ...
Standard Doclet version 1.6.0_16
Building tree for all the packages and classes ...
Generating doc\javadocbasic.html ...
Generating doc\package-frame.html ...
Generating doc\package-summary.html ...
Generating doc\package-tree.html ...
Generating doc\constant-values.html ...
Building index for all the packages and classes ...
Generating doc\overview-tree.html ...
Generating doc\index-all.html ...
Generating doc\deprecated-list.html ...
Building index for all classes ...
Generating doc\allclasses-frame.html ...
Generating doc\allclasses-noframe.html ...
Generating doc\index.html ...
Generating doc\help-doc.html ...
Generating doc\stylesheet.css ...
--[$] An example of using {@link}--
/**
* @author Lang Yu
* @see java.lang.String
*/
public class Javadoclink {
private int A;
private int B;
/**
* {@link #getAdd () Getadd ()} Method
* @return The result of (A + B)
**/
public int Getadd () {
return a + B;
}
}
Java Annotation Specification