Using the right comment in Java

Source: Internet
Author: User

Java provides three types of comments:

      • Single-line (c ++-style) Comments

        The simplest comment in Java is the single line comment. It starts with two forward slashes and continues to the end of the line. For example:

        Figure 1: single-line comments

        // This is a single-line commentX =1; // A single-line comment after code

    • Multi-line (c-style) Comments

Java also provides a comment type that can span multiple lines. you start this type of comment with a forward slash followed by an asterisk, and end it with an asterisk followed by a forward slash. the start and end delimiters for this type of comment may be on the same line, or they can be on different lines. for example

Figure 2: multi-line comments

 
/* This is a C-style comment *//* This is also a C-style comment, spanning multiple lines */

Note that C-style commentsCannotBe nested. Something like

Figure 3: bad comment nesting

/* A comment looks like/* This is a comment */Blah Blah Blah */

Will generated a syntax error because the Java compiler will only treat through the first */as a comment. (The comment ends at the first "*/" the compiler sees .)

YouCanNest single-line comments within multi-line comments:

Figure 4: good comment nesting

 
/* This is a single-line comment: // a single-line comment */

And multi-line comments in single-line comments:

Figure 5: multi-line comments in a single-line comment

/// * This is// A multi-line// Comment */

    • Documentation comments

      Documentation comments are special comments that look like multi-line comments but can be used to generate external documentation about your source code. These begin with a forward slash followedTwoAsterisks, and end with an asterisk followed by a forward slash. For example:

      Figure 6: Documentation comment

      /** This is a documentation comment *//** This is also a documentation comment */

      There are a few important things to note about documentation comments:

    • The documentation generator, javadoc, will add all text inside a documentation comment to an HTML paragraph. this means that any text inside a documentation comment will be formatted into a paragraph; spacing and line breaks are ignored. if you want special formatting, you must include HTML tags inside your documentation comment.
          • If a documentation Comment begins with more than two asterisks, javadoc assumes this is just used to create a "box" around the comment in the source code and ignores the extra asterisks. For example:

            Figure 7: Extra asterisks

            /********************************** This is the start A Method **********************************/

            Will only keep the text "this is the start of a method ".

          • Javadoc will ignore leading asterisks inside a documentation-Comment block. For example:

            Figure 8: Leading asterisks

            /*************************************** * This is a doc comment * on multiple lines that I want to stand * out in source code, looking "neat "************************************ ***/

            Will only keep the text "this is a doc comment on multiple lines that I want to stand out in source code, looking" neat ""

          • It's common practice to use something like

            Figure 9: Accidental javadoc comment

            /*************************************** ***... **************************************** **/

            To make a comment stand out. Be aware that this is treated as a documentation comment (even if that's not what you had intended), and cocould show up in the generated documentation.

When to use documentation comments

Documentation comments shocould (at very least) be used in front of every public class, interface, method and class/instance variable in your source code. this allows someone to run javadoc against the code and generate a simple document that lists the public entities and a brief description of each. you may also use documentation comments in front on non-public methods, and use a javadoc option to generate documentation for them. using documentation comments on non-public entities is not as important as publics (the interface isn' t exposed ...) but if you're commenting the codeAnywayYou might as well write those comments as documentation comments.

When to use single-line comments

Always!

My simple advice on commenting is that whenever you want to write a normal comment (not a documentation comment that describes and class, interface, method or variable) use a single line comment.

Why? Because you can easily use multi-line comments to "comment out" a section of your code! ("Commenting out code" refers to changing the lexical state of a section of source code to being inside a comment, making the compiler ignore that code.) take as an example:

Figure 10: code to comment out

 
X =1;/* Set X to 1 */Y =2;/* Set Y to 2 */F(X,Y); /* Call f with x and y */

If you want to comment out these three lines, you wocould either need to put a single line comment in front of each line:

Figure 11: commenting-out code using single-line comments

 
// X = 1;/* set X to 1 */// Y = 2;/* set Y to 2 */// F (x, y);/* call f with x and y */

Or add in multi-line comments wherever there isn't already one present:

Figure 12: commenting-out code using multi-line comments

 
/* X = 1 ;*/ /* Set X to 1 *//* Y = 2 ;*/ /* Set Y to 2 *//* F (x, y );*/ /* Call f with x and y */

Or mutilate/remove the "End comment" delimiters of the existing comments:

Figure 13: commenting-out code using multi-line comments (alternative)

 
/* X = 1;/* set X to 1 */y = 2;/* set Y to 2 */F (x, y ); /* call f with x and y */*/

None of these are terribly pleasant. It's much easier if the original code were:

Figure 14: Easier code to comment out

X =1;// Set X to 1Y =2;// Set Y to 2F(X,Y); // Call f with X and Y

Then you can easily comment it out by just placing a multi-line comment around it:

Figure 15: commenting-out single-line comments with a single multi-line comment

 
/* X = 1; // set X to 1y = 2; // set Y to 2f (x, y); // call f with x and y */

AlwaysUse single-line comments for your everyday commenting needs!

When to use multi-line comments

After reading the above section, this becomes obvious.OnlyUse multi-line comments to comment out sections of code.NeverUse themAnyOther purpose!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.