Nested comments?

Still a noob with IDEA but checked the archive and really did not find an answer - Is it intentional the way IDEA is handling nested comments?  That is it seems not to parse properly and keep track of the number of open and closing /* and */  ???   It seems the only way to get things to work is something like:

/*

start


*/

/*    inner comment

*/

/*

end

*/


so basically you need to add another level of comments else IDEA uses the first closing */ it finds to terminate the first /*    The "fix", however, is annoying when it comes time to uncomment as one then has to hunt and remove that extra  */ and /*

1 comment

beezle wrote:

Is it intentional the way IDEA is handling nested comments?  That is it seems not to parse properly and keep track of the number of open and closing /* and */  ???


Yes, that is intentional. IDEA is simply adhering to the Java language specification on comments.

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++).


. . .

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 /**.


The first highlighted point indicates that */ ends a comment regardless of the text before it (i.e. other comment opening characters). That is further clarified by the second highlighted point that comments do not nest. Try compiling, via javac, a class that has a nested comment. It will fail compiling.

You can nest // comments within a /*  */ comment.

Note that if you want to comment out code in IDEA, just select the code block and type Ctrl+/  It will comment out each line with a // comment. Any comments (or either type) are also "commented out".

{Edit} Fixed formatting

0

Please sign in to leave a comment.