Do different JDK Updates produce different Java byte code?

我怕爱的太早我们不能终老 提交于 2019-12-05 15:44:26

问题


A hypothetical scenario:

I've got a project whose source compliance level is specified to 1.5. Now I compile this project with two different JDKs: At first with JDK 6 Update 7 and then with JDK 6 Update 20.

Do these two different JDKs produce different Java byte code, although they only differ in their Update version?


回答1:


The generated code usually only differs in case of compiler bug fixes.

However the JLS does not specify a 1:1 mapping from source code to the generated byte code, so you should not rely on the exact same byte code to be generated.




回答2:


There is nothing that would stop the different versions from generating different bytecodes, as long as it's compliant to the behavior specified in the JLS. The JLS leaves many implementation details to vary from one implementation to another.




回答3:


Let's answer it from the other side: there is no guarantee that any two versions of the jdk produce the identical byte code. So you can expect differences in general.




回答4:


Why on Earth would someone go to the trouble of issuing an Update of a development kit if it didn't lead to changed byte code in at least some cases? I strongly doubt anyone would do it just for documentation updates.




回答5:


The bytecode could slightly differ, but this is nothing to worry about since it will be still compatible.

What really will be executed depends on JIT.




回答6:


The compiler of for example JDK 6 Update 7 might output slightly different bytecode than the compiler of JDK 6 Update 20, but since it's both Java 6, the class files will be fully compatible - you will be able to run code compiled with Update 20 on Update 7 without any problems.

Between major Java versions (for example Java 5 vs. Java 6) there might be changes so that code compiled on the newer version will not run on the older version. For example, for Java 7 there is most likely going to be a new instruction, invokedynamic. Class files that contain that instruction will not be runnable on older Java versions.

Such large changes are however never done between update versions.




回答7:


As usually is for different compilers, it is also in the Java case: the result must be the same but the way it is reached can be (from the bytecodes point of view) different., e.g. because of optimizations or alike. The JVM is stack based V machine; the following is an imaginary example (I don't know jvm mnemonics for the instructions opcodes)

push 10
push 20
add
push 19
push 1
add
push 10
add

These produce same result, but generated bytecodes are different (the first is slightly optimized, the second is "totally" not optimized; a third option could be push 30 since we are adding known (likely at compiletime) constants). This is a simple case, but more complex case can be easily built.




回答8:


If you compile with different JDK versions I would advice to use the target option to javac. Otherwise you may not be able to run the jar with an older JDK.

You may also want to use the source option to javac, in order to make sure the developer do not use classes/methods added in a more recent JDK.



来源:https://stackoverflow.com/questions/3012978/do-different-jdk-updates-produce-different-java-byte-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!