/** and /* in Java Comments

前端 未结 9 729
庸人自扰
庸人自扰 2020-11-28 19:22

What\'s the difference between

/**
 * comment
 *
 *
 */

and

/*
 * 
 * comment
 *
 */

in Java? When shou

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 19:50

    Comments in the following listing of Java Code are the greyed out characters:

    /** 
     * The HelloWorldApp class implements an application that
     * simply displays "Hello World!" to the standard output.
     */
    class HelloWorldApp {
        public static void main(String[] args) {
            System.out.println("Hello World!"); //Display the string.
        }
    }
    

    The Java language supports three kinds of comments:

    /* text */
    

    The compiler ignores everything from /* to */.

    /** documentation */
    

    This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.

    // text
    

    The compiler ignores everything from // to the end of the line.

    Now regarding when you should be using them:

    Use // text when you want to comment a single line of code.

    Use /* text */ when you want to comment multiple lines of code.

    Use /** documentation */ when you would want to add some info about the program that can be used for automatic generation of program documentation.

提交回复
热议问题