在 Java 中注释掉多行的语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2449304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java what is the syntax for commenting out multiple lines?
提问by David
In Java what is the syntax for commenting out multiple lines?
在 Java 中注释掉多行的语法是什么?
I want to do something like:
我想做类似的事情:
(comment)
LINES I WANT COMMENTED
LINES I WANT COMMENTED
LINES I WANT COMMENTED
(/comment)
采纳答案by Andrey
/*
LINES I WANT COMMENTED
LINES I WANT COMMENTED
LINES I WANT COMMENTED
*/
回答by zellio
With /**/
:
与/**/
:
/*
stuff to comment
*/
回答by CheesePls
/*
*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.
下面是所谓的 JavaDoc 注释,它允许您将某些标签(@return、@param 等)用于文档目的。
/**
*COMMENTED OUT STUFF HERE
*AND HERE
*/
More information on comments and conventions can be found here.
可以在此处找到有关注释和约定的更多信息。
回答by JonH
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]
*/
回答by kgrad
/*
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.
注意:像这样的多行注释不要嵌套。这可能是错误的来源。通常最好用 // 注释每一行。大多数 IDE 允许您非常简单地执行此操作。
回答by JeffH
As @kgrad says, /* */ does not nest and can cause errors. A better answer is:
正如@kgrad 所说, /* */ 不嵌套并且可能导致错误。更好的答案是:
// 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+\
大多数 IDE 有一个单一的键盘命令来执行/撤消此操作,因此真的没有理由再使用其他样式了。例如:在 eclipse 中,选择文本块并按 Ctrl+/
要撤消该类型的注释,请使用 Ctrl+\
UPDATE: The Sun coding conventionsays that this style should not be used for block textcomments:
更新:Sun 编码约定说这种样式不应用于块文本注释:
// 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:
但是 // 可以使用其他 3 种方式:
- A single line comment
- A comment at the end of a line of code
- Commenting out a block of code
- 单行注释
- 一行代码末尾的注释
- 注释掉一段代码
回答by Alfred
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 javadocif not already familiar because documenting your code is really important.
您的答案的简单问题已经回答了很多次:
/* LINES I WANT COMMENTED LINES I WANT COMMENTED LINES I WANT COMMENTED */
从你的问题看来,你想注释掉很多代码??我建议使用存储库(git/github)来管理您的文件,而不是注释掉行。
- 如果还不熟悉的话,我的最后一个建议是学习javadoc,因为记录您的代码非常重要。