Reputation:
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.
Upvotes: 5
Views: 1607
Reputation: 12684
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.
Upvotes: 0
Reputation: 7174
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.
Upvotes: 2
Reputation: 5394
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.
Upvotes: 0
Reputation: 1384
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: */ ";
Upvotes: 0
Reputation:
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.
Upvotes: 1