How exactly does java compilation take place?

前端 未结 9 990
执笔经年
执笔经年 2020-12-07 07:56

Confused by java compilation process

OK i know this: We write java source code, the compiler which is platform independent translates it into bytecode, then the jvm

9条回答
  •  旧巷少年郎
    2020-12-07 08:10

    The compiler was originally written in C with bits of C++ and I assume that it still is (why do you think the compiler is written in Java as well?). javac.exe is just the C/C++ code that is the compiler.

    As a side point you could write the compiler in java, but you're right, you have to avoid the chicken and egg problem. To do this you'd would typically write one or more bootstrapping tools in something like C to be able to compile the compiler.

    The .class file contains the bytecodes, the output of the javac compilation process and these are the instructions that tell the JVM what to do. At runtime these bytecodes have are translated to native CPU instructions (machine code) so they can execute on the specific hardware under the JVM.

    To complicate this a little, the JVM also optimises and caches machine code produced from the bytecodes to avoid repeatedly translating them. This is known as JIT compilation and occurs as the program is running and bytecodes are being interpreted.

提交回复
热议问题