有什么区别
/**
* comment
*
*
*/
和
/*
*
* comment
*
*/
在 Java 中?我应该什么时候使用它们?
最佳答案
第一种形式称为 Javadoc .当您为代码编写由 javadoc
工具生成的正式 API 时,您可以使用它。例如,the Java 7 API page使用 Javadoc 并由该工具生成。
您会在 Javadoc 中看到的一些常见元素包括:
@param
:用于指示传递给方法的参数,以及它们期望具有的值@return
:用于指示方法将返回什么结果@throws
:用于表示方法在某些输入的情况下抛出异常或错误@since
:用于指示该类或函数在其中可用的最早 Java 版本
例如,下面是 Integer
的 compare
方法的 Javadoc:
/**
* Compares two {@code int} values numerically.
* The value returned is identical to what would be returned by:
* <pre>
* Integer.valueOf(x).compareTo(Integer.valueOf(y))
* </pre>
*
* @param x the first {@code int} to compare
* @param y the second {@code int} to compare
* @return the value {@code 0} if {@code x == y};
* a value less than {@code 0} if {@code x < y}; and
* a value greater than {@code 0} if {@code x > y}
* @since 1.7
*/
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
第二种形式是 block (多行)注释。如果您想在评论中包含多行,请使用此选项。
我会说你只想谨慎地使用后一种形式;也就是说,您不希望使用没有描述方法/复杂函数应该具有的行为的 block 注释来使代码负担过重。
由于 Javadoc 在两者中更具描述性,并且您可以通过使用它来生成实际文档,因此使用 Javadoc 比简单的 block 注释更可取。
关于java -/** 和/* 在 Java 注释中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815636/