java 类文件包含注释 - 为什么?

标签 java

当我在 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/

相关文章:

java - Spring:无法确定正确的调用签名 - 多个过程/函数/签名

java - 如何制作计数器?

java - 离开应用程序时,必须删除 SharedPreferences

java - JSP :forward on button Click()(when target jsp is under web-inf folder) 中的问题

java - Android检查文件大小是否超过某个值

java - 在java中生成唯一的随机数

java - 在运行时更改哈希码

java - java中的自定义转换说明符

java - achartengine饼图未在 ScrollView 中呈现

java - 如何在单个布局中使用 RecyclerView 添加两个 Fragment 并滚动它们?