What is Java Architecture?
- In Java, there is the process of compilation and interpretation.
- Whatever code is written in Java, is converted into byte codes by the Java Compiler.
- These byte codes, in turn are converted into machine code by the Java Virtual Machine.
- The Machine code is executed directly by the machine in which the Java program runs.
- Hence, there is the process of compilation and interpretation.
- In addition to converting the compiled code into machine language code, the JVM provides an environment for running Java Programs.
How is Java Platform Independent?
- One of the main advantages of Java programs is that it is Platform independent.
- That is, the same java program can be run on different platforms without having to make any changes in the source code.
- You just have to make Java program in any one platform and the same program can be executed in any platform. Have you wondered how?
- The Java compiler compiles the source code and converts it into bytecode. This bytecode is stored in class files.
- Each platform has its own unique JVM.
- The process of interpreting the bytecode by the JVM and converting it into machine code is same for all JVMs regardless of the platforms.
- This makes Java Platform independent. Let us understand this with a diagram.
- The same bytecode is interpreted by two different JVMs of Windows and Linux and converted into machine code.
What is JRE?
- JRE stands for Java Runtime Environment and this is where the JVM resides along with all the class libraries and other supporting components.
- As explained earlier, the source code is converted into bytecode and this bytecode is stored in class files. When the program is run, the class file is loaded , verified and the JVM interprets the bytecode into machine code which will be executed by the machine in which the program runs.
- With respect to the above explanation, the JRE performs the following tasks.
- The JRE loads the class file(done by the class loader)
- The JRE verifies the bytecode.(done by the bytecode verifier)
- The JRE interprets the bytecode.(done by the JVM)
This is explained in the diagram below. A detailed Java Architecture can drawn as:
What are the components of JRE?
- All the class files required to run a Java program is loaded by the class loader.
- The Java program is made safe and secure by the Class loader as it separates the namespace of the classes obtained from the network and the namespace of the classes obtained locally.
- Once the byte code is loaded, the next step is to verify the bytecode which is done by the bytecode verifier.
Byte code Verifier.
- The bytecode verifier verifies the byte code to check if there are any security problems.
The following things are checked by the Bytecode verifier
1.Does the code follow JVM specifications.
- Is there any unauthorized access to memory made by the code.
- Does the code cause any stack overflows.
- Are there any invalid or illegal data conversions. (egs converting float into object type)
- Once this verification is successfully completed, the JVM converts this byte code into machine code which will be executed directly by the machine in which the Java Program runs.
Whats the role of Just in Time Compiler(JIT)?
- For those of you who are wondering what does JIT compiler(shown in Figure 3) do, well the JIT compiler helps in the faster execution of the Java Program. How does that happen?
- As we discussed earlier the bytecode is interpreted by the JVM and converted into machine code for the program to execute. This interpretation is a slow process.
- To overcome this problem, the JRE includes a completed called Just in Time(JIT) compiler. JIT makes the interpretation and execution faster.
Why the name “Just in Time”?
- If there is a JIT compiler library in the JRE, the JIT compiler compiles the bytecode into native machine code , when a particular bytecode is executed for the first time.
- This native machine code can be directly executed by the machine.
- Once the native machine code is recompiled by the JIT compiler, the execution time will be much lesser. This compilation happens when the bytecode is about to be executed and hence the name “Just in Time”.
- When the bytecode is compiled into a particular machine code, it is also cached by the JIT compiler and so , it can be reused for any future needs.
- So the performance improvement can be seen when the same code is executed again and again as the JIT uses the cached machine code.