Java is one of the most widely used programming languages, known for its platform independence and versatility. First, let’s explain how Java works. This will give us a better idea of what we are actually doing.
What Happens Before the Code Runs
The process of running Java code involves several steps. Let’s go through them one by one:
1. Writing the Code
Assuming there are no syntax errors. Just like when we talk to someone. You can say, “I have a dog or a cat,” and everyone will understand what you mean. Similarly, you could say “blufululu,” but no one would understand. The disadvantage compared to the real world is that a computer is strict, and if it doesn’t understand something, it simply stops interacting with us. As programmers, we must write Java code in a readable form, for example, the classic “Hello, World!”:
System.out.println("Hello, World!");
This code is saved in a file with a .java extension, e.g., HelloWorld.java.
2. Compiling the Code
Java uses a compiler to convert human-readable .java code into bytecode, which is a machine-independent intermediate format. The Java compiler, javac, generates a .class file from your .java file. For example, compiling HelloWorld.java creates HelloWorld.class.
3. Running Bytecode on the JVM
Code Execution Process
The .class file (bytecode) is then executed on the Java Virtual Machine (JVM). The JVM interprets the bytecode and runs it using the hardware.
What are JVM, JRE, and JDK?
It’s very easy to get confused about what is what, so let’s go through these terms.
Java Virtual Machine (JVM)
The JVM is the engine that runs your Java bytecode. It is responsible for:
• Loading bytecode
• Verifying bytecode for security
• Running bytecode
Each operating system (Windows, macOS, Linux, etc.) has its own JVM version, making JVM platform-dependent. However, bytecode is platform-independent, so Java programs can run on any platform with a compatible JVM.
Java Runtime Environment (JRE)
The JRE provides the environment needed to run Java applications. It includes:
• JVM
• Libraries and files required to run Java programs
When you install Java to run applications, you are installing the JRE.
Java Development Kit (JDK)
The JDK is a toolkit for developers used to create Java applications. It includes:
• The Java compiler (javac)
• Development tools
• The JRE (and therefore also the JVM)
If you are developing Java programs, you need the JDK. To simply run Java applications, the JRE is sufficient.
Key Features of Java
Platform Independence
Java is often described as “Write Once, Run Anywhere” (WORA). This means that once you write and compile a Java program, you can run it on any device or operating system with a JVM. This works because the compiled bytecode is platform-independent.
Role of the main Method
For the JVM to run a Java program, it looks for a main method with the following signature:
public static void main(String[] args)
This method serves as the program’s entry point. Without it, the JVM doesn’t know where to start.
Classes and Object-Oriented Programming
Java is an object-oriented programming language, which means that everything in Java revolves around objects and classes. Even a simple program requires at least one class.
Example of a Complete Java Program:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }}
Main Class Structure
Main File Structure
Compilation and Execution
• Save the code in a file named HelloWorld.java
• Compile it using: javac HelloWorld.java. This generates the HelloWorld.class file.
• Run the program using: java HelloWorld. This executes the bytecode and displays the output.
The JVM is designed for specific operating systems and hardware, making it platform-dependent. For example, the JVM for Windows differs from the JVM for macOS. However, since the JVM interprets platform-independent bytecode, Java programs can run on any system with a compatible JVM.
Java Architecture Layers
To summarize, here’s how Java’s architecture layers work:
• Hardware: Your physical machine (laptop, desktop, etc.)
• Operating System (OS): The system running on your hardware (Windows, Linux, etc.)
• JRE: Provides the environment for running Java programs