Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Comments Inside Text Block in Java 17 - Is that possible?

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

Since Java 15 Text Blocks are 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 to have comments inside a text block? I would like to tag some lines that came with new customers etc. Is this possible, will this be possible?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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
"""
... 
"""

However, as Federico klez Culloca mentioned in the comments, these are just workarounds. It is likely better to include a comment before the text block explaining what it does in general instead of writing comments in the text blocks.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!