The agenda of this blog is to have complete knowledge of Java Architecture and why Java is said to be a Platform Independent and what are the components of JAVA, and one more important feature of memory management
In other programming languages such as c and C++, there is a compiler to execute and run our code snippets. They are totally platform dependent. This means source code written in one Operating system cannot be executed in other operating systems.
Platform Dependency in C Language
Whenever we install C software, depending on the operating system we need to download and installed it. Every application is a standalone application including programming languages. So, all the programming languages are stand-alone applications. So, it is required to download and install it based on the operating system.
Whenever we install C, Compiler (to compile the application and generate machine code) + Library (to develop the application) will be installed in the system. The Windows compiler will work for the Windows operating system only and the MAC compiler will work only for the MAC operating system.
What is Platform Dependency?
Windows compiler will do a favor for Windows OS and the MAC compiler will do a favor for the MAC OS. This is known as Platform Dependency. That means if we develop one application using Windows Operating System that application can only be executed on Windows Operating System and can be run on other Operating Systems like Mac, and Linux, this is called platform dependency. And the language used for developing such applications is called Platform Dependent language. C and C++ are platform-dependent languages.
Let’s discuss how Java is working!!
How JAVA is platform independent?
The meaning of platform-independent is that JAVA compiled code can be executed on any Operating system.
The program written in the higher level language (human-understandable language) should be converted into Machine level language. Here comes the role of the compiler. Compiler covert the higher level language into machine level language (executable code or java Byte code).
Step-by-step procedure of Java Execution:
The java source code .java file, the javac compiles it.
The result of javac is the .class file which is byte code unlike c compiler, not a machine code.
The byte code generated is not exactly an executable code, it needs an interpreter to execute the .class file.
JVM is the Interpreter to execute a .class file.
Java was introduced with the collection of components like JDK, and JRE.JVM JIT makes Operating System independent.
Java has two different Environments to compile and run the source code, They are
1. Compile Time environment
2. Run-Time Environment
Compile Time Environment:
The code written in Java is converted into byte codes .class file which are done by the Java Compiler.
The run time environment is the environment in which a program or application is executed.
JRE is a set of components that will discuss below one by one.
JAVA Development Kit:
It is a Java development environment used to develop java applications. JDK comprises JRE, a compiler, a class loader, an interpreter, and some development tools in it.
JRE is the underlying technology that communicates between the JAVA program at the operating system.
The class loader is the subsystem of JRE that loads the class file whenever we run it.
Java Virtual Machine (JVM):
The main feature of JVM is WORA. WORA stands for Write once run anywhere.
That means JAVA code compiled in one OS can be executed anywhere without the restriction of OS and processor because of the JVM. The main task of JVM is to convert the Byte code into Machine level code.
JVM first of all loads the code in memory and verifies it. After that, it executes the code and provides a run time environment have its own Architecture given below.
The .class file can be executed in JVM.JVM is responsible for running Java applications in the same OS and provides a platform-independent environment for the code to run. This means that a Java application can run on any platform that has JVM installed, including Windows, macOS, and Linux.
JVM also Manages the memory in an efficient way.
Memory management in JAVA:
Do you know one thing?? How fair is the language manages its memory as much as the file size is less when file size leads to the program execution being faster and more efficient
JVM components play a vital role in memory management.
JVM is an abstract machine that provides the environment in which java byte codes (.class file) are executed.
Some sub-components to manage memory will discuss briefly below..
• Method area
• Heap area
• Stack area
• Native stack
In this memory, there is a place where the class data is stored. class method area holds the information of all static variables, static methods, and static blocks.
The heap area is generated when JVM starts up. it increases or decreases its size during application runs. It is the memory location for object and instance variables.
Stack area: Used to store all local variables and data used to call all methods, methods, and reference variables like objects.
It contains all the native methods used in our application.
It’s a central part of JVM. Its main task is to execute the class file or byte code that is generated on the compilation The execution engine has three main components to execute.
Interpreter, JIT compiler, and Garbage collector
It converts the byte code into native code and executes it. It sequentially executes the code. The interpreter interprets continuously and even the same method multiple times. This reduces the performance of the system, and to solve this, the JIT compiler is introduced.
JIT compiler is introduced to remove the drawback of the interpreter. It increases the speed of execution and improves performance.
The garbage collector is used to manage the memory, and it is a program written in Java. It works in two phases, i.e., Mark and Sweep. Mark is an area where the garbage colle
ctor identifies the unused chunks of memory. The Sweep removes the identified object from the Mark
Java Native Interface:
Java Native Interface works as a mediator between Java method calls and native libraries.