Home > Mobile >  Comments Inside Text Block in Java 17 - Is that possible?
Comments Inside Text Block in Java 17 - Is that possible?

Time:12-02

A simple question. Is it possible to have comments in Text Blocks in Java 17?

With Java 17.0.1 Text Blocks are now an option:

"""
   Hello Hello                    //how to create a comment here? - this was wanted by customer XXX
This dog has a big nose        // This line came with version 1.0.2
This cat needs to eat
"""

Is it possible? I would like to tag some lines that came with new customers etc. Is this possible, will this be possible?

CodePudding user response:

The Java Language Specification states that:

The lexical grammar implies that comments do not occur within character literals, string literals, or text blocks

(emphasis mine)

So nope, you can't do that.

CodePudding user response:

I don't know specially for Java but I would think that no it isn't possible to do this as some people would want to have text with // in the strings without escaping.

CodePudding user response:

As of JEP 378, it is not possible to add a comment in a text block.

However, you can use a few tricks:

For example, you can replace everything inside of /*... /*:

"""
abc /* this is a comment*/
def //another comment
""".replace("/\\*.*\*/",""). replace("//.*$","");

Another possibility would be to use multiple text blocks:

"""
... 
"""
  //comment
"""
... 
"""
  •  Tags:  
  • java
  • Related