我有以下Java类

public class HelloWorld {
  public static void main(String []args) {
  }
}

当我编译这个文件并对得到的类文件运行sha256时

9c8d09e27ea78319ddb85fcf4f8085aa7762b0ab36dc5ba5fd000dccb63960ff  HelloWorld.class

接下来我修改了类,并添加了一个空行,像这样:

public class HelloWorld {

  public static void main(String []args) {
  }
}

我再次在输出上运行sha256,期望得到相同的结果,但我得到了

11f7ad3ad03eb9e0bb7bfa3b97bbe0f17d31194d8d92cc683cfbd7852e2d189f  HelloWorld.class

我在这篇TutorialsPoint文章中读到:

只包含空白(可能带有注释)的行称为空行,Java完全忽略它。

所以我的问题是,既然Java忽略空行,为什么两个程序的编译字节码不同?

也就是说,在HelloWorld.class中,0x03字节被0x04字节取代。


基本上,行号是为了调试而保留的,因此,如果您按照您所做的方式更改源代码,您的方法将从不同的行开始,而已编译的类将反映出差异。


您可以使用javap -v查看更改,它将输出详细信息。像其他已经提到的一样,差异将体现在行号上:

$ javap -v HelloWorld.class > with-line.txt
$ javap -v HelloWorld.class > no-line.txt
$ diff -C 1 no-line.txt with-line.txt
*** no-line.txt 2018-10-03 11:43:32.719400000 +0100
--- with-line.txt       2018-10-03 11:43:04.378500000 +0100
***************
*** 2,4 ****
    Last modified 03-Oct-2018; size 373 bytes
!   MD5 checksum 058baea07fb787bdd81c3fb3f9c586bc
    Compiled from "HelloWorld.java"
--- 2,4 ----
    Last modified 03-Oct-2018; size 373 bytes
!   MD5 checksum 435dbce605c21f84dda48de1a76e961f
    Compiled from "HelloWorld.java"
***************
*** 50,52 ****
        LineNumberTable:
!         line 3: 0
        LocalVariableTable:
--- 50,52 ----
        LineNumberTable:
!         line 4: 0
        LocalVariableTable:

更准确地说,类文件在LineNumberTable部分中不同:

The LineNumberTable attribute is an optional variable-length attribute in the attributes table of a Code attribute (§4.7.3). It may be used by debuggers to determine which part of the code array corresponds to a given line number in the original source file. If multiple LineNumberTable attributes are present in the attributes table of a Code attribute, then they may appear in any order. There may be more than one LineNumberTable attribute per line of a source file in the attributes table of a Code attribute. That is, LineNumberTable attributes may together represent a given line of a source file, and need not be one-to-one with source lines.


除了用于调试的行号细节外,清单还可以存储构建时间和日期。每次编译时,这自然是不同的。


“Java忽略空行”的假设是错误的。下面是一个代码片段,它的行为取决于main方法前的空行数:

class NewlineDependent {

  public static void main(String[] args) {
    int i = Thread.currentThread().getStackTrace()[1].getLineNumber();
    System.out.println((new String[]{"foo", "bar"})[((i % 2) + 2) % 2]);
  }
}

如果在main前没有空行,则输出"foo",但在main前有空行,则输出"bar"。

由于运行时行为不同,所以.class文件必须不同,而不考虑任何时间戳或其他元数据。

这适用于所有可以访问行号为堆栈帧的语言,而不仅仅是Java。

注意:如果它是用-g:none编译的(没有任何调试信息),那么行号将不会被包括在内,getLineNumber()总是返回-1,并且程序总是打印“bar”,而不管换行的数量。