Compiler and interpreter:
The translation from source code to machine code is done by Compiler. The compiler also performs some operations Compiler checks the Syntax It optimizes our code and Generate Machine Code Once Machine code is generated, it is executed very fast. So Compilation is faster. The interpreter has no compilation step, it directly executes the Source code. The interpreter executes the Program line by line.
Compilation: Faster execution than interpreter and platform dependent
Interpreter: slow execution but platform independent
Java is both compilation and interpretation language. How is this possible?
Developers write code in Java. After compiling the code, the code is generated to javJavatecode. In c and c++ compiler converts the code into machine code but in java, it translates the code into java bytecode. Now, what is java bytecode? Bytecode is compiled object code of java program. JVM understands bytecode which means that JVM executes bytecode. After executing bytecode we get our output.
Suppose I am creating a program with the name of Test.java and after compilation it, is created Test.class. This .class file is our java bytecode and Test .class can be executed by JVM on any machine on which platform JVM is specifically installed. And JVM Execute bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because the configuration of each OS differs. But, Java is platform independent. There are three notions of the JVM: specification, implementation, and instance.
- A specification where the working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
- Implementation of JVM is known as Java Runtime Environment.
- Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.
Internal Architecture of JVM
Let’s understand the internal architecture of JVM.
Java virtual machine contains classloader it acts as a subsystem of JVM and used to load class files.
2) Method Area
Class Area stores per-class structures such as the runtime method data, constant pool, the code for methods and field.
It is the runtime data area in which objects are allocated.
Then JVM contains stack used to store frames.
|Each thread has a private JVM stack, created at the same time as a thread.
It also holds local variables, and act as a part in method invocation and return.
5) Program Counter Register
Program counter register contains the address of the JVM instruction currently being executed.
6) Native Method Stack
It contains all the native methods used in the application.
7) Execution Engine
|1) A virtual processor|
|2) Interpreter: It is used to read bytecode and then gives the output by executing the inbstructions.|
|3) Just-In-Time(JIT) compiler: Inside jvm, there is JIT compiler instead of an interpreter we use JIT to speed up execution.JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. JIT is used to improve the performance.
JRE is java runtime environment. If u want to run Java Applications. U have to install JRE. For example, browsers have JRE by default
JRE includes JVM AND API. Java API is java library. Now, what is jdk? Java development kit. We need jdk to develop and run a java program.
Jdk comes with some development tools e.g compiler but we also need JRE to run programs. IF u want to run java programs u need to install jdk.
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.