Will Java ever allow comments to nest?

Pavel Rappo pavel.rappo at oracle.com
Wed Jul 1 20:49:41 UTC 2020


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.


[1] https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-3.7

More information about the discuss mailing list