Will Java ever allow comments to nest?

Martin Buchholz martinrb at google.com
Wed Jul 1 22:28:25 UTC 2020


I have an ancient perl script that can __reliably__ extract comments
from Java (and C) sources with a regex.

That would not be possible if comments were nested.

Comment nesting is one language design decision to only get to make once.

On Wed, Jul 1, 2020 at 1:53 PM Pavel Rappo <pavel.rappo at oracle.com> wrote:
>
> Hello,
>
> JLS specifies that comments do not nest [1]. For example, the below source will not compile
>
>     $ cat CommentsDoNotNest.java
>     public class CommentsDoNotNest {
>         /* /* */ */
>     }
>
>     $ javac CommentsDoNotNest.java
>     CommentsDoNotNest.java:2: error: illegal start of type
>         /* /* */ */
>                  ^
>     1 error
>
> Some modern languages allow comments to nest. For example, Swift, F#, and Haskell do that. Why doesn't Java do that? Will Java ever do that?
>
> I can see cases where nesting would be useful:
>
> 1. Commenting out long blocks of code that possibly contain `/* ... */` comments.
> 2. Having a character sequence resembling a comment terminator `*/`, for example, in a regex or a glob pattern.
> 3. Having a `/* ... */` comment within a doc comment `/** ... */`. This is typically used in doc comments of language modeling APIs and, admittedly, is a niche case.
>
> All of the above can be done today, albeit with much friction.
>
> Thanks,
> -Pavel
>
> [1] https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-3.7
>


More information about the discuss mailing list