Do multi-line comments in Java for strings?

This question is probably applicable also to other languages โ€‹โ€‹with C-like multi-line comments. Here is the problem I am facing. I work with Java code in Eclipse and I would like to comment on a block of code. However, there is a line that contains the sequence of characters "* /", and Eclipse believes that the comment should end there, although it is inside the line. It gives me a lot of mistakes and fails to build.

/* ... some Java code ... ... "... */ ..." ... ... more Java code ... */ 

Does the Java specification match the Eclipse interpretation of my multi-line comment? I would like to think that Java and / or Eclipse will be responsible for such things.

+4
source share
6 answers

Eclipse is true. There is no interpretation context in the comment (without escaping, etc.). See JLS ยง3.7 .

+9
source

In Eclipse, you can select the part of the source code that you want to comment on, and use the combination Ctrl + / for a single-line comment of each line in the selected section - puts "//" at the beginning of the lines.

Or, if you really want to block the comment, use the combination Ctrl + Shift + /. It will detect block comments in your selection. However, destroying this is more complicated than single-line comments.

+2
source

Yes, I comment on the code to do a quick test. I have already tested what I need by commenting on the code in another way; I was just wondering what seems like a weird Java and / or Eclipse bug.

+1
source

A simple test shows that Eclipse is correct:

 public class Test { public static final void main(String[] args) throws Exception { String s = "This is the original string."; /* This is commented out. s = "This is the end of a comment: */ "; */ System.out.println(s); } } 

This does not compile with:

 Test.java:5: unclosed string literal s = "This is the end of a comment: */ "; 
0
source

It may be useful for me to simply make a "batch" multi-line comment so that it comments on each line with "//". This is Ctrl + "/" in Idea to comment and decode selected lines, Eclipse should have a similar function.

0
source

I often use only // for inline commments and use /* */ only to comment on large blocks like you do.

Many developers will still use / * * / for inline comments, because what they are familiar with, but they all run into problems like this, in C it didnโ€™t matter, because you could # if 0 leaves.

0
source

Source: https://habr.com/ru/post/1276470/


All Articles