Chapter[5]: What is JVM (Java Virtual Machine) and How args
Works in Java?
Understanding the Java Virtual Machine (JVM) and how args
works in main method Java can seem complex at first, but let’s simplify it with practical examples.
What is the JVM (Java Virtual Machine)?
The JVM is like an engine that runs Java programs. Here’s how it works:
1: Java Code:
- You write your Java code in
.java
files.
2: Compilation:
- The Java Compiler (
javac
) converts your.java
file into a special format called bytecode (a.class
file).
3: Execution:
- The JVM takes the bytecode and runs it on your computer.
- It translates the bytecode into machine code that your operating system understands.
Think of the JVM as a translator that makes your Java program work on any computer, regardless of the operating system (Windows, Mac, Linux, etc.).
.java (Java Source Code)
↓
Compiler (javac)
↓
.class (Bytecode)
↓
JVM (Java Virtual Machine)
↓
Machine (Execution on Hardware)
Why is the JVM Important?
- Platform Independence: Java programs can run anywhere, thanks to the JVM. This is why Java is called “Write Once, Run Anywhere” (WORA).
- Memory Management: The JVM handles tasks like allocating and freeing memory automatically, so you don’t have to worry about it.
- Security: The JVM has built-in security features to protect your system from malicious code.
How args
Works in Java?
The args
parameter in the main
method allows you to pass data to your Java program when you run it. Let’s break it down:
1: What is args
?
- It’s an array of strings (
String[]
), where each element stores one piece of data you provide during program execution.
2: How Does It Work?
- When you run your Java program from the command line, you can add extra inputs (arguments).
- The JVM collects these inputs and puts them into the
args
array.
3: Example of args
:
- If you run this command:
public class cmdargs {
public static void main(String[] args) {
// Check the length of the args array and print each argument
System.out.println("First argument: " + args[0]);
System.out.println("Second Argument: " + args[1]);
}
}
Run the program using the below command:
- javac cmdargs.java
- java cmdargs hello World
First argument: hello
Second Argument: world
args[0]
will store"hello"
, andargs[1]
will store"world"
.
Java Architecture and How Everything Fits Together
Here’s a step-by-step explanation of how a Java program works, focusing on the role of the JVM and args
.
- You Write Code:
- Example code:
public class cmdargs {
public static void main(String[] args) {
// Check the length of the args array and print each argument
System.out.println("First argument: " + args[0]);
System.out.println("Second Argument: " + args[1]);
}
}
1: Compile the Code:
- Use
javac cmdargs.java
to convert your.java
file into a.class
file (bytecode).
2: Run the Program:
- Use
java cmdargs hello world
to execute the program. - The JVM:
Reads the bytecode from cmdargs.class
.
Translates it into machine code.
Executes the program.
Diagram: Java Program Execution Flow
Step 1: Write Code
cmdargs.java
|
v
Step 2: Compile Code
javac (Java Compiler)
|
v
Bytecode (cmdargs.class)
|
v
Step 3: Execute Program
JVM (Java Virtual Machine)
|
v
Machine Code (Runs on CPU)
Detailed Breakdown of the Code
public class cmdargs
:
- Creates a class called
cmdargs
.
public static void main(String[] args)
:
- Entry point of the program.
args
collects any data passed when running the program.
System.out.println()
:
- Prints messages or data to the console.
for (String arg : args)
:
- Loops through each item in the
args
array and prints it.
Key Takeaways
1: JVM:
- The JVM runs your Java program by converting bytecode into machine code.
- It makes Java programs platform-independent.
2: args:
- Allows you to pass dynamic input to your program during execution.
- Stored as an array of strings.
3: System.out.println:
- Prints information to the console, making it easy to debug and display outputs.
You can now catch the podcast on YouTube too!
.
.
.
Happy Coding!