Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where exactly does the compilation of source code to byte code happen in Java?

I did some search trying to understand how the java source files are executed. I could not find a clear answer illustrating the steps from beginning to end in JRE and JDK jargon. So I am writing what I understand from different blogs, but some blanks do exists. Corrections on my understanding are most welcome. Two questions marked Q1 and Q2 are below point 2.

  1. write a HellowWorld.java file

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

  3. JRE contains JVM and other libraries to create runtime environment. JVM(which by itself is platform dependent) executes the bytecode to machine code. Just-in-time compiler which is actually the part of JVM does the real compilation part of bytecode to machine code, plus caching byte codes if necessary.

  4. garbage collection is encompassed in JRE.
like image 877
eagertoLearn Avatar asked Sep 25 '13 23:09

eagertoLearn


People also ask

How source code is converted to byte code in Java?

How is Byte Code generated? Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. The Interpreter can also be called JVM(Java Virtual Machine).

How source code is converted to byte code?

The Java compiler (javac) converts the source code into bytecode. Bytecode is a kind of average machine language. This bytecode file (. class file) can be run on any operating system by using the Java interpreter (java) for that platform.

Where is Java bytecode executed?

Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM).

Which command is used for compiling a Java source code to produce a byte code?

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files.


2 Answers

The compilation to "bytecode" is done by javac, the Java compiler. And the difference between the JDK (Java Development Kit) and the JRE (Java Runtime Environment) is essentially that the JDK includes javac, while the JRE does not.

The compilation to bytecode form is a true compilation -- the bytecode format does not at all resemble the original source. But the bytecode must be either interpreted or further compiled in order to run on most hardware systems. (A few experimental hardware systems have been built that can directly execute a form of bytecode.)

On most systems the bytecodes start out being interpreted (by, duh, the "Java interpreter", which is part of the JRE). As the code executes, the "hot" parts of the bytecode are compiled by a "just-in-time compiler" (JITC -- also part of the JRE), and then execute with essentially the same efficiency as C++ or another "directly compiled" language.

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

like image 187
Hot Licks Avatar answered Nov 04 '22 07:11

Hot Licks


Your class file is byte-codes. Those codes are the same for all Java virtual machines. That's the point, the standard library / runtime environment must be to some degree platform specific (as it bridges those differences), you don't worry about that. The Java compiler generates the bytecode, it is not a part of the runtime environment. For the same reasons your classes and such are not part of the compiler, it just reads them.

So yes .class file = bytecode form.

like image 39
Alec Teal Avatar answered Nov 04 '22 07:11

Alec Teal