Where exactly is the source code compiled into byte code in Java?

I tried to understand how java source files are executed. I could not find a clear answer illustrating the steps from start to finish in JRE and JDK jargon. Therefore, I write what I understand from different blogs, but some spaces exist. Corrections in my understanding are welcome. The two questions marked Q1 and Q2 are below point 2.

  • write the file HellowWorld.java

  • javac HelloWowrld.java gives HelloWorld.class. That is, it gives a class file, which is byte code. Now I can take this bytecode generated on the Mac and go to the Windows machine and run it, which should work fine.
    Q1: Now this compilation is in byte code, is it really compilation or is it interpreted? Q2: Should Javac be part of the JDK and NOT JRE?

  • The JRE contains JVMs and other libraries for creating the runtime. The JVM (which in itself is platform dependent) executes bytecode for machine code. The just-in-time compiler, which is actually part of the JVM, does the real part of compiling bytecode for machine code, and also caches bytecodes if necessary.

  • garbage collection covers JRE.
+4
source share
2 answers

Compilation into bytecode is done using javac , the Java compiler. And the difference between the JDK (Java Development Kit) and the JRE (Java Runtime Environment) is that the JDK includes javac and the JRE does not.

Compilation in the form of a bytecode is a true compilation - the bytecode format is completely different from the original source . But the bytecode must be interpreted or compiled to work on most hardware systems. (Several experimental hardware systems have been built that can directly take the form of bytecode.)

On most systems, bytecodes begin to be interpreted (via, duh, the "Java interpreter", which is part of the JRE). As the code executes, the "hot" parts of the bytecode are compiled by the "compiler" exactly at the point in time (JITC is also part of the JRE), and then executed with the same efficiency as C ++ or another "directly compiled" language.

It should be noted that the bytecode format is very similar to the "intermediate language" formats used by many traditional "two-phase" / "optimizing" compilers. In this sense, javac is the first part of a traditional compiler.

+4
source

Your class file is byte codes . These codes are the same for all Java virtual machines. That this point, the standard library / runtime environment should be platform specific to some extent (since it eliminates these differences), you don’t worry about that. The Java compiler generates bytecode; it is not part of the runtime. For the same reasons, your classes, etc. They are not part of the compiler, they just read them.

So yes .class file = bytecode form.

+2
source

All Articles