Hello and welcome to the second installment of the blog series "The DSA Handbook". In this post, I'll explain the Java programming language, how it works, its architecture, and much more. So stick with me until the end.
Also if you're interested in learning more about DSA, I highly recommend checking out this amazing playlist on YouTube called "Java + DSA + Interview Preparation Course" by Kunal Kushwaha .
Now, let's get started...
Java is a popular and powerful programming language that is widely used in building large-scale applications. It is object-oriented, meaning that it is based on the concept of objects, which can contain data and code that manipulate that data. Java is also class-based, meaning that objects are created using pre-defined templates called classes.
How Java code executes :
We can provide this bytecode (.class file) to any system means we can compile the Java code on any system.
But JVM is platform depentent means for every operating system the executable file that we get, has some step-by-step set of instruction dependent on platform.
What is Platform Independence ?
It means that the byte code can run on all operating systems.
We need to convert source code to machine code so that the computer can understand
Compiler helps in doing this by turning it into executable code
This executable code is a set of instructions for the computer
After compiling C/C++ code we get exe file which is platform dependent
In Java we get bytecode, JVM converts this to machine code
Java is platform-independent but JVM is platform dependent
Architecture of Java :
JDK (Java Development Kit)
It provides environment to develop and run the Java programs.
It is a package that includes
JRE -> to execute your program
Some development tools -> to provide an environment to develop your program
Compiler -> javac
Archiver -> jar
docs generator -> javadoc
interpreter/loader.
JRE (Java Runtime Environment)
It is an installation package that provides an environment to only run the program.
It consists of -
Development technologies
User interface toolkit
Integration libraries
Base libraries
JVM
After we get the .class file, the things happen at runtime are -
Class loader loads all classes needed to execute the program.
JVM send code to bytecode verifier to check the format of code.
How JVM works (class loader) ?
Loading
Reads .class file and generate binary data.
An object of this class is generated in the heap.
Linking
JVM verifies .class file
Allocates memony for class variables & default values
Replace symbolic references fom the type with direct references
Initialization
- All static variables are assigned with their values defined in the code and static block.
JVM contains the Stack and Heap memory allocations.
JVM execution
Interpreter :
Line-by-line execution.
when one method is called many times, it will interpret again and again.
JIT :
those methods that are repeated, JIT provides direct machine code so re-interpretation is not required.
makes execution faster.
Garbage collector
Note : Whatever work JVM is doing, it is doing with the help of JRE. Like the libraries JVM needs, JRE will provide it. JRE is like a box which has JVM - the actual content in the box, along with some other development tools.