views:

760

answers:

7

This question would probably apply equally as well to other languages with C-like multi-line comments. Here's the problem I'm encountering. I'm working with Java code in Eclipse, and I wanted to comment out a block of code. However, there is a string that contains the character sequence "*/", and Eclipse thinks that the comment should end there, even though it is inside a string. It gives me tons of errors and fails to build.

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

Does the Java specification match with Eclipse's interpretation of my multi-line comment? I would like to think that Java and/or Eclipse would account for this sort of thing.

A: 

Are you commenting out the code just to test something? I know this isn't answering your question at all but instead of commenting out code I always delete. Version control accomplishes the same thing that commenting out code does.

bpapa
+8  A: 

Eclipse is correct. There is no interpretation context inside a comment (no escaping, etc). See JLS §3.7.

Damien B
+1  A: 

Yes, I am commenting the code out just to do a quick test. I've already tested what I needed to by commenting the code out another way; I was just curious about what appears to be an odd misfeature of Java and/or Eclipse.

Daniel F. Hanson
A: 

A simple test shows 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 fails to compile with:

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

I may be helpful to just do a "batch" multiline comment so that it comments each line with "//". It is Ctrl+"/" in Idea for commenting and uncommenting the selected lines, Eclipse should have a similar feature.

axk
+1  A: 

In Eclipse you can highlight the part of the source code you want to comment out and use the Ctrl+/ to single-line comment every line in the highlighted section - puts a "//" at the beginning of the lines.

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

Cem Catikkas
A: 

I often use only // for inline commments, and use /* */ only for commenting out large blocks the way you have.

A lot of developers will still use /* */ for inline comments, because that's what they're familiar with, but they all run into problems like this one, in C it didn't matter as much because you could #if 0 the stuff away.

davenpcj