📜  Java中的注释

📅  最后修改于: 2022-05-13 01:54:32.283000             🧑  作者: Mango

Java中的注释

在程序中,注释通过放置所涉及的代码细节来使程序变得更易于阅读,正确使用注释可以使维护更容易并容易发现错误。编译代码时,编译器会忽略注释。
Java中的注释分为三种:

  1. 单行注释。
  2. 多行注释。
  3. 文档评论。

单行注释

初级程序员主要使用单行注释来描述代码功能。它是最简单的打字评论。
句法:

//Comments here( Text in this line only is considered as comment )

例子:

Java
//Java program to show single line comments
class Scomment
{
    public static void main(String args[])
    {
         // Single line comment here
         System.out.println("Single line comment above");
    }
}


Java
//Java program to show multi line comments
class Scomment
{
    public static void main(String args[])
    {
        System.out.println("Multi line comments below");
        /*Comment line 1
          Comment line 2
          Comment line 3*/
    }
}


Java
//Java program to illustrate frequently used
// Comment tags
 
/**
* 

Find average of three numbers!

* 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.     * @param args Unused.     * @return Nothing.     */       public static void main(String args[])     {         FindAvg obj = new FindAvg();         int avg = obj.findAvg(10, 20, 30);           System.out.println("Average of 10, 20 and 30 is :" + avg);     } }


多行注释

要在代码或复杂的代码片段中描述完整的方法,单行注释可能会很乏味,因为我们必须在每一行都给出“//”。因此,可以使用多行注释来克服这种情况。
句法:

/*Comment starts
continues
continues
.
.
.
Comment ends*/

例子:

Java

//Java program to show multi line comments
class Scomment
{
    public static void main(String args[])
    {
        System.out.println("Multi line comments below");
        /*Comment line 1
          Comment line 2
          Comment line 3*/
    }
}

我们也可以使用上面的语法来完成单行注释,如下所示:

/*Comment line 1*/

文档评论

这种类型的注释通常在为项目/软件包编写代码时使用,因为它有助于生成一个文档页面以供参考,该页面可用于获取有关当前方法、其参数等的信息。
例如 http://docs.oracle.com/javase/7/docs/api/ Java/util/Scanner.html 是一个自动生成的文档页面,它是通过使用文档注释和用于处理注释的 javadoc 工具生成的。
句法:

/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used 
*such as 

* *comment ends*/

可用的标签:

TagDescriptionSyntax
@authorAdds the author of a class.@author name-text
{@code}Displays text in code font without interpreting the text as HTML markup or nested javadoc tags.{@code text}
{@docRoot}Represents the relative path to the generated document’s root directory from any generated page.{@docRoot}
@deprecatedAdds a comment indicating that this API should no longer be used.@deprecated deprecatedtext
@exceptionAdds a Throws subheading to the generated documentation, with the classname and description text.@exception class-name description
{@inheritDoc}Inherits a comment from the nearest inheritable class or implementable interface.Inherits a comment from the immediate surperclass.
{@link}Inserts an in-line link with the visible text label that points to the documentation for the specified package, class, or member name of a referenced class.{@link package.class#member label}
{@linkplain}Identical to {@link}, except the link’s label is displayed in plain text than code font.{@linkplain package.class#member label}
@paramAdds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section.@param parameter-name description
@returnAdds a “Returns” section with the description text.@return description
@seeAdds a “See Also” heading with a link or text entry that points to reference.@see reference
@serialUsed in the doc comment for a default serializable field.@serial field-description | include | exclude
@serialDataDocuments the data written by the writeObject( ) or writeExternal( ) methods.@serialData data-description
@serialFieldDocuments an ObjectStreamField component.@serialField field-name field-type field-description
@sinceAdds a “Since” heading with the specified since-text to the generated documentation.@since release
@throwsThe @throws and @exception tags are synonyms.@throws class-name description
{@value}When {@value} is used in the doc comment of a static field, it displays the value of that constant.{@value package.class#field}
@versionAdds a “Version” subheading with the specified version-text to the generated docs when the -version option is used.@version version-text

Java

//Java program to illustrate frequently used
// Comment tags
 
/**
* 

Find average of three numbers!

* 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.     * @param args Unused.     * @return Nothing.     */       public static void main(String args[])     {         FindAvg obj = new FindAvg();         int avg = obj.findAvg(10, 20, 30);           System.out.println("Average of 10, 20 and 30 is :" + avg);     } }

输出:

Average of 10, 20 and 30 is :20

对于上述代码文档,可以使用工具 'javadoc' 生成:
可以通过在终端中运行以下命令来使用 Javadoc。

javadoc FindAvg.java