How Java Actually Works Underneath the Hood 🖥️
{JVM, JRE & JDK Clearly Explained}
If you have ever stumbled upon a Java code in your lifespan & wondered, how exactly does this small line of code:
public class JavaBasics{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
generates the output "Hello World!", In this blog post you'll understand how Java code is executed underneath the Hood, because Java is one of the most Roubust and popular languages (for some reason), you need to have a fundamental knowledge of what is JVM, JRE, JDK, their difference & why does everyone keep Saying that Java System Independent:
Code - Computer Interaction
Any code you write is essentially an instruction that you pass to the Computer so that it generates a desired response. So your code deals with the computer's hardware but the catch here is that the computer doesn't understand English or even any coding language for that matter, so we need something called Compiler (Javac) which compiles your Java code into Byte Code (created as .class file as soon as you run your code) which is basically lines of 0s and 1s hence it is unreadable by humans.
JVM
This byte code is then sent to a Java Virtual Machine which is a Unit responsible for executing your Java code, All it needs is a "main()" method to execute all your entire program, and in order to do that it requires a special signature which goes as follows: " public static void main(String arg)", sounds familiar isn't it?
What makes Java System Independent?
Now, The JVM is built on top of an OS (Operating System) which is in turn built upon top of & Interacts with The Computer Hardware, so In order to Run a Java Code all you need is a JVM on your device irrespective of what system you are using be it macOS, Windows, Linux, Symbian, etc.
So JVM is responsible for the Execution of your Java Program.
So what is JRE and JDK??
Now, every time you are not just going to write "Hello World" command, what if you come across a complicated feature that you need to implement, You need some Libraries! These libraries needed to be compiled during runtime so There is another Unit / Layer on top of JVM called Java Runtime Environment (JRE) which contains all the built-in liberties.
So basically : JVM + Built-in Libraries = JRE
If you are a developer who writes Java Codes Then you will have one mode layer on top of it called JDK (Java Development Kit), but if let's say you want to run the code you wrote on your Friend's computer then all you need is JRE which has JVM inside it JDK is not required, Hence Java is WORA (Write Once Run Anywhere) provided you have JRE & JVM Installed.
So that's it guys If you have made it till here, you now understand how Java works Behind the scenes, If you are new to Java or someone who is learning some frameworks like Siring Boot & advanced concepts like Microservices it's always good to keep track of the basics & fundamentals.
I hope this post helps you, I'll be posting weekly content on Android Development, Java, Cloud Computing & AI/ML consider Subscribing to my newsletter to get awesome complex Concepts explained into simple terms.