Java Compiler
Java Virtual Machine (JVM)
Java Runtime environment(JRE)
JIT Compiler
How they works together
Java Compiler API
Java Compiler and Java Runtime Environment
When you learn Java, it is important to know how your Java code is changed and run by the computer. This process involves some important parts like the Java Compiler, Java Virtual Machine (JVM), Java Runtime Environment (JRE), and Just-In-Time (JIT) Compiler. Let us see how they all work together.
What is a Java Compiler
A Java compiler is a tool that changes your Java code (written in files ending with .java) into a different form called bytecode (saved in .class files). This bytecode is not made for any one computer. It can run on any computer that has the Java Virtual Machine. The most commonly used compiler comes with the Java Development Kit or JDK. Some old compilers, like the GNU Compiler for Java, used to change Java code directly into machine code for a specific system. But today, Java compilers usually create bytecode.
What is the Java Virtual Machine (JVM)
The JVM runs the bytecode created by the compiler. It acts like a middleman between your Java program and the computer. When you run a Java program, the JVM loads the bytecode and either reads it line by line or changes it into machine code using the JIT compiler.
What is the Java Runtime Environment (JRE)
The JRE is a software package that helps run Java programs. It includes the JVM, Java libraries, and other tools needed to run Java. If you only want to run Java programs (not write or compile them), the JRE is enough. But for writing and compiling programs, you need the JDK.
What is the Just-In-Time or JIT Compiler
The JIT compiler is part of the JVM. It helps make Java programs faster. When the JVM sees that some parts of the program are used again and again, the JIT compiler changes those parts into machine code. This saves time and makes the program run more quickly.
How These Parts Work Together
- You write your Java program and save it in a file with .java extension.
- The Java compiler changes your code into bytecode and saves it in a .class file.
- The JVM loads this bytecode and runs it. The JIT compiler may change some parts into machine code to speed things up.
- The JRE provides everything needed to run the program.
Java Compiler API or JSR 199
Java also has a special feature called the Java Compiler API (JSR 199). This allows developers to use the compiler inside their own Java programs. It is useful for creating tools like code editors and compilers.
Conclusion
Java uses several important parts to run programs smoothly on any system. The compiler changes the code into bytecode, which is then run by the JVM. The JIT compiler helps speed up the program while it runs. The JRE provides all the tools and libraries needed to support the program. All these parts work together to make Java a powerful and portable programming language.