Brief comments: often the more simple the problem is more easily ignored by people.

The problem

In a popular article on Stack Overflow this month, a foreign developer asked: “Why do I get two different bytecode files when I just add a blank line to a Java class?”

Now you have a Java class

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

The bytecode. Class file is first compiled from this class and sha256 translated to get it

9c8d09e27ea78319ddb85fcf4f8085aa7762b0ab36dc5ba5fd000dccb63960ff  HelloWorld.classCopy the code

Then add an empty line to the same Java class

public class HelloWorld {

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

Do sha256 translation of the compiled bytecode file again

11f7ad3ad03eb9e0bb7bfa3b97bbe0f17d31194d8d92cc683cfbd7852e2d189f  HelloWorld.classCopy the code

Oh, my God, they’re different, because empty lines affect how the code works.

Of course, blank lines don’t affect how the code runs, so why compile a different.class bytecode file?

Analysis of the

When I throw this problem to the side of the program ape friends, we first use Base64 to translate the file also obtained different data; Oh, no, base64 will not be calculated into the file compilation time and other file information; Test MD5 encrypted.class file or different data; People began to think that the compiler had made a mistake and that the blank lines should be optimized.



I suggest you give it a try and think about why

The answer

The answer is simple:

Why do you think Java will throw a specific number of lines when an error is reported? In fact, Java will record the number of lines for debugging purposes.

The simpler the problem, the easier it is to ignore.


Original link:

Why does a Java class compile differently with a blank line?stackoverflow.com

Recommended reading:

JoshLi: Google is going to charge European Android device makers for their app serviceszhuanlan.zhihu.com
JoshLi: React Native vs. Cordova.zhuanlan.zhihu.com
JoshLi: The major browsers have announced that they will stop supporting TLS 1.0 and TLS 1.1.zhuanlan.zhihu.com

Aurora Daily, aurora Developer’s media.

Read three English technical articles every day.