Does Java produce object code or byte code?

前端 未结 5 2044
南笙
南笙 2020-12-19 07:04

It\'s my understanding that the Java compiler produces \"byte code\", not \"object code\". First of all, is this correct?

Also, that\'s just what my book says, I was

相关标签:
5条回答
  • 2020-12-19 07:44

    C object code is in a form that only the OS on which it was compiled can interpret.

    C code -> C compiler(e.g. gcc) -> object code -> os

    The C compiler outputs object code for the OS that it was written for. If you want your C code to run on another machine, you have to recompile it.
    C code compiled on Windows won't run on linux without recompiling.

    Java code-> Java compiler(javac) -> byte code -> jvm

    The Java compiler puts out byte code that runs on its JVM. All the idiosyncracies of the OS are pushed down into the JVM, hidden from the Java code. So as long as your OS has a JVM, you can run Java bytecode on it without recompiling. Java code compiled on Windows can run on any other OS with the same version of JVM.

    Also refer difference between object code and byte code

    0 讨论(0)
  • 2020-12-19 08:00

    Byte code is just the "object code" for the Java Virtual Machine. It's not native code (e.g. x86). To be honest, I rarely hear the term "object code" these days - it's generally clearer to talk in more specific terms.

    0 讨论(0)
  • 2020-12-19 08:00

    Java is platform independent. When you compile Java source code it is converted to byte code and when the application is run the JRE on the client machine executes the byte code.

    0 讨论(0)
  • 2020-12-19 08:04

    Java compiler (javac.exe) takes in Java source (.java) files as input and outputs Java bytecode (.class) files (if your code compiles successfully). You then execute the Java launcher (java.exe) and point it to your bytecode. It is then up to the Java virtual machine (JVM) to take your code the rest of the way, and convert your bytecode into machine (or native) code.

    You can think of it as object code being closer to the machine code, and bytecode as being further from the machine code. Also, bytecode is portable (which is one of the big advantages of Java language), while machine code needs to be "ported" by the means of a manual process (i.e. re-compiling for a different target platform or architecture).

    Object code is "almost" machine code, it's kind of in the middle. It's like a portion of the resulting machine code. Because of the portability of Java language, you can't have the compiler translate your source code directly into machine code. Because the machine code needs to be different for different platforms and architectures. If someone runs your bytecode on Windows, JVM will translate your bytecode into one kind of machine code. If someone runs your bytecode on a Mac OS X, JVM will translate your bytecode into a different kind of machine code.

    You can think of the JVM as an interpreter for your bytecode. Although, technically it's called JIT (Just In Time) compilation. The JVM is part of the JRE (Java Runtime Environment) and while Java programs are pretty much platform independent (any system that cun run JVM can also run Java programs), the JVM part of the JRE is platform-dependent.

    The short version:

    • Java compiler produces bytecode (compilation half the way, platform independent, cannot run yet).
    • Java virtual machine produces machine code.
    • Object code is a portion of the resulting machine code.
    • Bytecode becomes object code at JIT time.
    • Object code becomes machine code (compilation rest of the way, platform dependent, can run).

    This may not be accurate 100% as I am also just starting to learn this stuff. But this is my understanding of this concept so far. I hope this helps.

    0 讨论(0)
  • 2020-12-19 08:05

    When you compile a java program, it goes to byte-code. When you run the resulting artifact, the JVM of the platform then runs (well, interprets) the bytecode, turning it into machine level instructions.

    0 讨论(0)
提交回复
热议问题