How Java code is executed?
The java file is compiled into .class file with the help of a compiler named “javac”. The entire file is then converted from a human readable form to a byte code. This .class file which includes the byte code is then line by line interpreted into machine code with the help of JVM(Java Virtual Machine) and run on the system.
.class file :
1)This code will directly run on the system.
2) We need JVM to run this.
3)This is the reason why Java is platform independent.
Q) What does it mean by platform independence?
- Byte Code can run on any OS.
- It is required to convert source code to machine code so that comp can understand.
- Compiler helps by turning it into executable code.
Architecture of Java
JDK VS JRE VS JVM
JDK(Java Development Kit) : Provides Environment to run the Java Program.
JRE(Java Runtime Environment): It is an installation package that provides environment to only run the program.
JVM(Java Virtual Machine): Helps in line by line execution.
JIT(Just In Time): Makes execution faster.
Java source code is compiled into Bytecode with the help of Javac compiler which is inside JDK. The bytecode furthermore is passed onto JVM where it executes the code line by line using the library tools inside JRE and converts it into machine code.
How JVM works:
Class Loader consists of 3 steps:
- Loading: It reads the .class file and generate binary data(a object of this class is created in heap)
- Linking
- Initialization: All static variables are are assigned with their values defined in code.
JVM contains the stack and heap memory allocations.