views:

135

answers:

8

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?

+7  A: 

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.

Joachim Sauer
A: 

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.

Kilian Foth
To answer your rhetorical question: Because the interpretation/execution of the byte code as well as the libraries may have changed. *Most* changes to the JDK between updates are to the runtime and library and *not* to the compiler.
Joachim Sauer
+2  A: 

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.

Andreas_D
+7  A: 

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.

polygenelubricants
+1  A: 

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.

stacker
+1  A: 

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.

ShinTakezou
+1  A: 

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.

Jesper
A: 

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.

Anthony