Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will compiling the same code using different JDKs result in the same byte code?

Tags:

java

Will the byte code be the same if the same java file is compiled by different Java compilers? No changes are done to the Source Java File.

like image 369
Narayana Murthy Avatar asked May 04 '12 06:05

Narayana Murthy


1 Answers

Will the byte code be the same if the same java file is compiled on different JVMs? No changes are done to the Source Java File.

It is not entirely clear what you mean, but the answer is most likely "no".

  • Different JDKs will have different Java compilers which may emit different bytecodes for the same source code. The javac compiler has evolved over time.

  • Different major versions of the Java often emit class files that conform to different versions of the classfile specification.

  • Even if you restrict yourself to one JDK installation, two runs of the compiler on the same source file will produce non-identical .class files. (The class file includes a compilation timestamp ...)

The only way that the answer could be "yes" would be if you were ignoring the compilation timestamp and (possibly) other metadata in the comparison, you were emitting bytecodes for the same target version, and the JDK versions were close enough that the Java compilers bytecode generation hadn't change between the versions.

like image 138
Stephen C Avatar answered Sep 20 '22 04:09

Stephen C