Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

3.7 Comments

There are two kinds of comments:

/* text */

A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++).

// text

A end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored (as in C++).

These comments are formally specified by the following productions:

    
   Comment:
       
   TraditionalComment
       
   EndOfLineComment

    
   TraditionalComment:
       / * CommentTail
    
   EndOfLineComment:
       / / CharactersInLineopt
   

    
   CommentTail:
       * CommentTailStar
       
   NotStar CommentTail

    
   CommentTailStar:
       /
       * CommentTailStar
       
   NotStarNotSlash CommentTail

    
   NotStar:
       
   InputCharacter 
   but not *
       LineTerminator

    
   NotStarNotSlash:
       
   InputCharacter 
   but not * or /
       LineTerminator

    
   CharactersInLine:
       
   InputCharacter
       
   CharactersInLine InputCharacter

These productions imply all of the following properties:

  • Comments do not nest.
  • /* and */ have no special meaning in comments that begin with //.
  • // has no special meaning in comments that begin with /* or /**.

As a result, the text:

    /* this comment /* // /** ends here: */

is a single complete comment.

The lexical grammar implies that comments do not occur within character literals (§3.10.4) or string literals (§3.10.5).

  • + Share This
  • 🔖 Save To Your Account