Oct 29, 2023
Photo by Nicolas Arnold on Unsplash
Ifyou're a Java developer, you've likely encountered the trio of "J"s - JVM, JRE, and JDK. While it's tempting to gloss over these terms and focus on coding, having a grasp of these fundamentals can save you from potential pitfalls and embarrassment when working with the language.
In this article, we'll explore the significance of these "J"s and unravel their meanings. But first, let's start by decoding their full forms:
The JVM stands as the cornerstone of the Java language. It's the place where your Java programs come to life. To illustrate its role, let's craft a straightforward Java program and delve into how these components interconnect.
Open your text editor and enter the following simple "Hello World" message, then save it as HelloWorld.java:
1public class HelloWorld {
2 public static void main(String[] args) {
3 System.out.println("Hello World!");
4 }
5}
As you may know, our computers don't comprehend English or any human language for that matter; they speak the language of 0s and 1s. To bridge this gap, we need to convert our program into a format that the computer can understand.
This transformation process is known as compilation, and it's handled by the javac, also known as the Java Compiler. To compile the program, execute the following command:
1$ javac HelloWorld.java
After running this command, you'll find a new file generated HelloWorld.class.
If you'd like to inspect the contents of the .class file, you can use online tools like hexed.it or iamkate.com/code/binary-file-viewer
It's a bunch of garbage to you but not to the computer.
Now, let's try to run it. To run it, type the following command:
1$ java HelloWorld
1Hello World
The entire process can be broken down into three fundamental steps:
Bytecode is an intermediate code generated by a compiler after compiling the source code. It's not specific to any physical machine and comprises binary, hexadecimal, and macro instructions (e.g., new, add, swap, etc.). However, the CPU doesn't directly understand bytecode. To bridge this gap, an interpreter is required to convert Java bytecode into machine code.
Flowchart of the Process
The key idea here is that there are codes understood only by specific machines. For instance, adding two numbers, such as 3 and 5, would involve different operations on different machines. Here are hypothetical examples of machine code for x86 and ARM architectures:
1x86 machine code: 90 24 40 00 12
1ARM machine code: 01 00 A0 E3
Note: These are random numbers used for illustration.
The interpreter's role is to translate bytecode, which is independent of the machine, into machine code, which is machine-dependent.
JVM is tasked with the important responsibility of translating Java bytecode into machine-specific instructions that the underlying hardware can comprehend and execute. It is this feature of the JVM that provides platform independence, allowing Java programs to run on any device or operating system with a compatible JVM implementation.
The JVM comprises various components that store critical data information for the Java program, including:
It's important to note that the JVM is not a monolithic entity implemented by a single vendor. There are various JVM implementations for different platforms, such as the Oracle HotSpot JVM, OpenJDK, and more. While each implementation may have unique features and optimizations, they must adhere to the JVM specification and pass a compatibility test suite known as the Java Compatibility Kit (JCK).
When you execute java HelloWorld, the JVM performs several crucial tasks:
Image taken from https://www.geeksforgeeks.org/
The Java Runtime Environment (JRE) comprises a collection of software tools and libraries that enable Java applications to operate on various computer systems and devices. Think of it as an operating system of sorts, facilitating coordination among different hardware components, with one of the most pivotal components being the Java Virtual Machine (JVM).
In summary, the Java Runtime Environment (JRE) plays a vital role in the Java platform, allowing Java applications to run on a diverse array of devices and operating systems. It encompasses the Java Virtual Machine (JVM) and a collection of class libraries, enabling Java programs to execute in a platform-independent manner.
The Java Development Kit (JDK) serves as a comprehensive software development package that equips developers with the tools and libraries necessary for building Java applications.
The JDK encompasses the following essential components:
The JDK is an essential kit for Java developers, enabling them to create, compile, debug, document, and package Java applications effectively. It is a versatile toolkit that forms the backbone of Java software development.
In the world of Java development, understanding the roles of the three "J"s - JVM, JRE, and JDK - is more than a matter of convenience; it's a fundamental necessity. These components form the bedrock of the Java platform and are crucial for both developers and end-users.
The Java Virtual Machine (JVM) serves as the heart of the Java language, executing Java programs by translating bytecode into machine-specific instructions. It provides the foundation for Java's platform independence and facilitates its "write once, run anywhere" promise.
The Java Runtime Environment (JRE) complements the JVM by providing the necessary runtime environment for Java applications to run seamlessly. It houses not only the JVM but also a comprehensive set of class libraries and tools, creating a robust environment for Java programs.
On the developer's side, the Java Development Kit (JDK) empowers the creation of Java applications. It includes the JRE, the indispensable Java compiler (javac), a debugger (jdb), and other essential tools for building, testing, and documenting Java software.
In essence, the JVM, JRE, and JDK work in harmony to make Java a versatile and powerful programming language. The JVM takes Java bytecode and translates it into machine-specific instructions, while the JRE provides the runtime environment for these applications. Meanwhile, the JDK equips developers with the necessary tools and libraries to create, test, and deploy Java applications effectively.
Whether you're a seasoned Java developer or just starting on your programming journey, grasping the significance of these "J"s is a fundamental step toward becoming a proficient Java practitioner. With this knowledge, you'll not only write better Java code but also appreciate the magic that happens behind the scenes when you run a Java application.
Subscribe to the newsletter to learn more about the decentralized web, AI and technology.
Please be respectful!