我只是想知道在java编程语言中单行和多行注释有什么区别
多行注释
/*
* This is line 1
* This is line 2
/*
单行注释
// This is line 1
// This is line 2
那么,从.java到.class的程序翻译来看,这两个注释的区别是什么(或者换句话说,为什么我们需要单行注释,当我们已经有多行注释时)。我已经知道编译器在Lexical Analyzer阶段删除注释。
这只是为了方便程序员。例如,当我在写一段注释时,我不希望在每行前面加上//
。此外,/* */
允许您放置注释'inline',
System.out.println(2 + /* inline comment */ 2);
编译器和程序员没有区别。
如果我必须写更长的注释,我使用/**/
。
//
非常适合在调试时注释掉代码行,特别是当您有几行代码并且您想取消注释中间的一行时
// statement 1
// statement 2
statement 3
// statement 4
比
更容易实现/* statement 1
statement 2
*/
statement 3
/*
statement 4
*/
但最终还是要看个人品味。
没有区别。这取决于用户的偏好。
但是,这些语法的特殊变体由Javadoc解释以生成文档。
/**
* Short one line description.
*
* Longer description. If there were any, it would be
* here.
* <p>
* And even more explanations to follow in consecutive
* paragraphs separated by HTML paragraph breaks.
*
* @param variable Description text text text.
* @return Description text text text.
* @throws IOException Explanation of why this exception is thrown.
*/
没有任何区别。所有的注释都被删除,所以在执行时,不管它们的格式如何,注释都不会影响程序。这是为了方便。
这是我的建议
单
单行注释用于描述类、接口、方法或变量。
多个
只使用多行注释来注释掉部分代码