Narayana Murthy
Narayana Murthy

Reputation: 61

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

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.

Upvotes: 6

Views: 2306

Answers (7)

Stephen C
Stephen C

Reputation: 719729

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.

Upvotes: 5

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

It is JVM specific as well as JVM resides on which platform(OS). In each case it is somewhat different. I don't know about compiling same source twice using same javac on same jvm. Does anyone have idea?

Upvotes: 0

Michael Schmeißer
Michael Schmeißer

Reputation: 3417

The byte code is produced by the Java compiler which is not part of the specific JVM implementation. Byte code is the name of the intermediate language executed by a JVM's execution engine, so the JVM does not produce but run this code. What the JVM can do, is compile the byte code into machine code which can differ. This compiler is called Just-in-time (JIT) compiler.

What can lead to different byte code, however, is different Java compilers - the major ones are listed there: http://en.wikipedia.org/wiki/Java_compiler

Upvotes: 2

amitp
amitp

Reputation: 71

No, With every new JDK. Generally Java compiler is also updated to do some optimization while compiling.the byte code will be different when compiled with different Java versions.

Upvotes: 0

gd1
gd1

Reputation: 11413

Not at all.

PS.: probably you mean "JDK", not "JVM"

Upvotes: 3

Thihara
Thihara

Reputation: 6969

Nope. Byte code will differ on which compiler you use it in. but only slightly and within spec. I may be wrong but as far as I know the changes are mostly related to how the javac in different JDKs optimizes. Byte code is anyway JVM interoperable.

Upvotes: 2

popfalushi
popfalushi

Reputation: 1362

Yes, but: you can experience trouble if you use packages like com.sun etc., because they proprietary.
Moreover, some time ago i stumbled with Object(In|Out)putStream working differentely in Sun JDK and IBM JDK, however, maybe, I did something wrong there, so I am not sure in this.
Proof: http://docs.oracle.com/javase/specs/ - The Java Virtual Machine Specification, Java SE 7 Edition
Update: Sorry. Indeed, not exactly the same bytecode, but all certified JVMs will understand it and give the same result.

Upvotes: -1

Related Questions