Java基础 专题
专题目录
您的位置:java > Java基础专题 > Java注释
Java注释
作者:--    发布时间:2019-11-20

java注释是不会被编译器和解释器执行的语句。 注释可以用于提供关于变量,方法,类或任何语句的信息或解释。 它也可以用于在特定时间隐藏程序代码。

java注释的类型

在java中有3种类型的注释。它们分别如下 -

  1. 单行注释
  2. 多行注释
  3. 文档注释

1)java单行注释

单行注释仅用于注释一行,它使用的是 // 两个字符作为一行注释的开始,如下语法所示 -

语法:

// this is single line comment

示例:

public class commentexample1 {
    public static void main(string[] args) {
        int i = 10;// here, i is a variable
        system.out.println(i);
        int j = 20;
        // system.out.println(j); 这是另一行注释,这行代码不会被执行。
    }
}

上面示例代码输出结果如下 -

10

2)java多行注释

多行注释用于注释多行代码。它以 /* 开始,并以 */ 结束,在 /**/之间的代码块就是一个注释块,其中的代码是不会这被执行的。

语法:

/* 
this  
is  
multi line  
comment 
*/

示例:

public class commentexample2 {
    public static void main(string[] args) {
        /*
         * let's declare and print variable in java.
         *
         *  这是多行注释
         */
        int i = 10;
        system.out.println(i);
    }
}

上面示例代码输出结果如下 -

10

3)java文档注释

文档注释用于创建文档api。 要创建文档api,需要使用javadoc工具。

语法:

/** 
this  
is  
documentation  
comment 
*/

示例:

/**
 * the calculator class provides methods to get addition and subtraction of
 * given 2 numbers.
 */
public class calculator {
    /** the add() method returns addition of given numbers. */
    public static int add(int a, int b) {
        return a + b;
    }

    /** the sub() method returns subtraction of given numbers. */
    public static int sub(int a, int b) {
        return a - b;
    }
}

通过javac工具编译:

javac calculator.java

通过javadoc工具创建文档api:

javadoc calculator.java

现在,将在当前目录中为上面的calculator类创建了html文件。 打开html文件,并查看通过文档注释提供的calculator类的说明。如下所示 -


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册