tags:

views:

348

answers:

7

In Java what is the syntax for commenting out multiple lines?

I want to do something like:

(comment)
LINES I WANT COMMENTED
LINES I WANT COMMENTED
LINES I WANT COMMENTED
(/comment)
+4  A: 

With /**/:

/*
stuff to comment
*/
Mimisbrunnr
+12  A: 
/* 
LINES I WANT COMMENTED 
LINES I WANT COMMENTED 
LINES I WANT COMMENTED 
*/
Andrey
+3  A: 
/*
 *STUFF HERE
 */

or you can use // on every line.

Below is what is called a JavaDoc comment which allows you to use certain tags (@return, @param, etc...) for documentation purposes.

   /**
    *COMMENTED OUT STUFF HERE
    *AND HERE
    */

More information on comments and conventions can be found here.

CheesePls
That is a JavaDoc entry, not a comment.
rodrigoap
yea did on accident and was fixing while you commented
CheesePls
and it still is
David
The first is a JavaDoc comment yes, the second is a simple block comment. ~fixing again so no one gets confused~
CheesePls
+4  A: 

You could use /* begin comment and end it with */

Or you can simply use // across each line (not recommended)

/*
Here is an article you could of read that tells you all about how to comment
on multiple lines too!:

[http://java.sun.com/docs/codeconv/html/CodeConventions.doc4.html][1]
*/
JonH
+8  A: 

/* Lines to be commented */

NB: multiline comments like this DO NOT NEST. This can be the source of errors. It is generally better to just comment every line with //. Most IDEs allow you to do this quite simply.

kgrad
..which is to be done with `Ctrl`+`/` in Eclipse. To uncomment, hit it once again. You dan do it for multiple selected lines.
BalusC
@BalusC - same in netbeans
kgrad
Good to know. I don't use it, so I didn't mention about it :)
BalusC
+4  A: 

As @kgrad says, /* */ does not nest and can cause errors. A better answer is:

// LINE *of code* I WANT COMMENTED 
// LINE *of code* I WANT COMMENTED 
// LINE *of code* I WANT COMMENTED 

Most IDEs have a single keyboard command for doing/undoing this, so there's really no reason to use the other style any more. For example: in eclipse, select the block of text and hit Ctrl+/
To undo that type of comment, use Ctrl+\

UPDATE: The Sun coding convention says that this should style not be used for block text comments:

// Using the slash-slash
// style of comment as shown
// in this paragraph of non-code text is 
// against the coding convention.

but // can be used 3 other ways:

  1. A single line comment
  2. A comment at the end of a line of code
  3. Commenting out a block of code
JeffH
+2  A: 
  • The simple question to your answer is already answered a lot of times:

    /*
    LINES I WANT COMMENTED
    LINES I WANT COMMENTED
    LINES I WANT COMMENTED
    */

  • From your question it sounds like you want to comment out a lot of code?? I would advise to use a repository(git/github) to manage your files instead of commenting out lines.

  • My last advice would be to learn about javadoc if not allready familiar because documenting your code is really important.
Alfred