Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested multiline comments in java

Tags:

java

comments

While writing experimental code, I find it very useful to comment out entire blocks of code at a time. However, i can't find a reasonable way to do this in Java, because I frequently end up with nested blocks being commented out.

In C or C#, this is easily achieved using #if 0 or #if false

I can't seem to find any such equivalent in Java -- any help would be appreciated. Thanks!

like image 639
Shezan Baig Avatar asked Aug 21 '11 15:08

Shezan Baig


People also ask

Can a multi line comment be nested Java?

Nested comments is not natively a feature of java. So what can you do? Here are some different options: Slash Slash comment nesting The best I've been able to come up with is to use an editor that has a hotkey to slashslash comment out an entire block of code as //'s can be nested.

Can a multi line comment be nested?

Multi-line comments are visible to Scheme as a single whitespace. Multi-line comments may be nested: Every #| within the comment starts a new multi-line comment, which has to be terminated by a matching |# .

What is nested comments in Java?

A nested comment is a comment inside another comment. In Java you can't nest a multiline comment into another multiline comment, however you can nest a single line comment into a multiline comment (but does this make sense?) /* This is a comment // this is a nested comment This is another comment */

Can we Nest comments in Java?

Single line comments can be nested inside of multi-line comments. However, multi-line comments cannot be nested inside of other multi-line comments.


3 Answers

I'm presuming you are wanting to comment out nested code for debugging or testing purposes, right? Because leaving huge blocks of commented out code in production code is generally considered very bad style.

Nested comments is not natively a feature of java. So what can you do? Here are some different options:

Slash Slash comment nesting The best I've been able to come up with is to use an editor that has a hotkey to slashslash comment out an entire block of code as //'s can be nested. Eclipse and Netbeans both have this support, though there are minor differences in how it works between IDEs, as far as when it decides to toggle the comments vs nest them. Usually nesting // comments can be done so long as the selections are not identical.

third party preprocessor That said, J2ME uses a pre-processor in eclipse, but I don't think that would help with desktop java. There do appear to be other preprocessors written for or compatible with eclipse if you search for them. However, that definitely would hinder portability as that's not a standard java feature.

modify comment Another thing I've often done for a quick nested comment need (moreso in css which doesn't do // comments than java though) is to add a space between the final two characters ending the inner comment */ so that java will not end the outer comment at the inner comment's end. Not too difficult (but slightly manual) to restore the original comment. The nested /* is usually ignored by the parser based on how it does its pattern matching. Eg /* code /* nested comment * / more code */`

use version control You could also easily use most any version control system to save a copy of your code with the code to remove. And then later use the version control restore/diff/merge to get your removed code back. Generally this solution is considered the best style. Especially if this commented out code will be needed to be left that way for long amounts of time.

if (false) If your code that you want to mass prevent from running both compiles and is within a single function, you can easily disable it by adding an if statement around it that the condition will never be true such as if (false) { ... } Obviously, this would never pass any sort of lint type code inspection tool, but its quick and easy ;-)

like image 93
Jessica Brown Avatar answered Oct 18 '22 00:10

Jessica Brown


Comments are not meant for leaving dead code - they are meant to describe live code.

For the rare cases that this is needed, using an IDE to comment the lines that you want (CTRL + / in eclipse) is sufficient.

If this is a common practice, rethink your process and your code. And feel free to delete code and commit it to SCM. It won't get lost if you need it.

like image 20
Bozho Avatar answered Oct 18 '22 00:10

Bozho


You could just to if(false){}. Java doesn't really percompile the way C or C++ does but the Java compiler should be smart enough to skip that entire section (once it's determined that this if statement will never evaluate to true). This will only work to comment out code within functions.

Or, if you have a good IDE like eclipse or notepad++ you can use the built-in shortcut keys to comment out multiple lines using //.

Eclipse = ctrl + / (repress to uncomment) N++ = ctrl + k (shift + crtl + k to uncomment)

like image 2
JSideris Avatar answered Oct 18 '22 02:10

JSideris