当我在 Eclipse 编辑器中打开 MyClass.class
文件时,它会显示注释和代码。
.class
文件难道不应该优化去掉注释吗?我期待看到没有注释的优化代码。
我在 Eclipse 中打开 String.class 时看到了这个。
/**
* Returns a hash code for this string. The hash code for a
* <code>String</code> object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using <code>int</code> arithmetic, where <code>s[i]</code> is the
* <i>i</i>th character of the string, <code>n</code> is the length of
* the string, and <code>^</code> indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
最佳答案
当 Eclipse 打开一个 .class
文件时,它会生成字节代码和注释的可读表示(它可以在何处以及它认为它们会有所帮助的地方)。这些注释不是 .class
文件的一部分。 (但是,如果调试信息存在于 .class
文件中,它将在生成注释时使用它。)
编辑:根据您更新的问题,我想说发生的事情是 Eclipse 将源文件与 .class
文件相关联并显示该文件而不是字节代码。对于 Java API 类,这通常是因为 Eclipse 中的库定义设置为指向源 .jar 文件。
关于java 类文件包含注释 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30178037/