Search code examples
javajvmbytecode

How Java Platform Independence will work?


I have executed a java program on a 64-bit compiler and generated the byte code for that program. Is it possible to run the same byte code on a 32-bit compiler without loss of the data?

In my program I have declared a variable x=10024 on 64-bit compiler?
Then what will be the value of x in 32-bit compiler? If the value of x is same, How is it Possible without loss of data? Can you please elaborate?


Solution

  • When you run the "32-bit" compiler, you are running the compiler code in a 32-bit JVM, and when you run the "64-bit" compiler you are running exactly the same code in a 64-bit JVM which will produce exactly the same byte code. If the byte code is not exactly the same, you have found a bug. The only difference is that the "64-bit" version might run slightly faster (as much as 5% at a guess)

    I have executed a java program on a 64-bit compiler

    You compile byte code in a compiler, not run it.

    and generated the byte code for that program.

    Byte code is not 32-bit or 64-bit and it makes no difference how you created it, or what JVM you ran the compiler in.

    Is it possible to run the same byte code on a 32-bit compiler without loss of the data?

    You can compile code with classes compiled by any version of Java up to the same one, regardless of whether it was 32-bit or 64-bit.

    In my program I have declared a variable x=10024 on 64-bit compiler?

    That doesn't make any sense. You might have compiled code like int x=10024; using a compiler running in a 64-bit JVM. How it was compiled make no difference whatsoever.

    Then what will be the value of x in 32-bit compiler?

    The same as for a compiler running in a 32-bit JVM, or any other JVM.

    If the value of x is same, How is it Possible without loss of data?

    There is no reason for there to be a loss of data. If x is an int it will be a 32-bit signed value regardless of how you compile the code or which JVM was used to run the compiler.