Java Fundamentals: Understanding Java, JVM, and Your First Program
Introduction
If you're beginning your programming journey, Java is one of the most popular and beginner-friendly programming languages to learn. It is widely used in software development, coding interviews, backend systems, Android applications, and enterprise software.
Before diving into Data Structures and Algorithms (DSA), it is important to understand the fundamentals of Java and how Java programs actually run behind the scenes.
In this article, we'll explore what Java is, why it's widely used, how Java executes programs, the role of JVM, JRE, and JDK, and finally write our very first Java program.
What is Java?#
Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It was designed with a simple goal:
Write code once and run it anywhere.
Unlike many programming languages that depend heavily on the operating system, Java programs can run on different platforms without requiring any changes to the source code.
This feature is known as Write Once, Run Anywhere (WORA).
Today, Java powers millions of applications worldwide and remains one of the most in-demand programming languages.
Why Learn Java?#
Java continues to be one of the most preferred programming languages for beginners and professionals because it provides the perfect balance between simplicity, performance, and industry relevance.
Some reasons to learn Java are:
- Easy to learn and beginner-friendly
- Platform independent
- Strong Object-Oriented Programming support
- Large developer community
- Rich collection of libraries and frameworks
- Widely used in coding interviews and placements
- Strong demand in the software industry
Java vs Other Popular Languages#
As a beginner, you might wonder why we are learning Java instead of languages like C, C++, or Python.
Java vs C#
| C | Java |
|---|---|
| Procedural Programming Language | Object-Oriented Programming Language |
| Platform Dependent | Platform Independent |
| Manual Memory Management | Automatic Memory Management |
| No JVM Support | Uses JVM |
Java vs C++#
| C++ | Java |
|---|---|
| Supports Pointers | Hides Pointers from Developers |
| Manual Memory Management | Automatic Garbage Collection |
| Platform Dependent | Platform Independent |
| More Complex Syntax | Simpler and Cleaner Syntax |
Java vs Python#
| Python | Java |
|---|---|
| Simpler Syntax | More Structured Syntax |
| Interpreted Language | Compiled and Executed Through JVM |
| Popular in AI and Data Science | Popular in Enterprise and Backend Development |
| Generally Slower | Generally Faster |
Each language has its own strengths, but Java provides a strong foundation for programming concepts, making it an excellent choice for learning DSA and software development.
Applications of Java#
Java is used in many areas of software development.
Some common applications include:
- Web Applications
- Android Applications
- Desktop Applications
- Enterprise Software
- Banking Systems
- Backend Development
- Cloud-Based Applications
Its versatility is one of the main reasons for its popularity.
How Java Programs Run#
When we write a Java program, the computer cannot directly understand the code we write.
Java follows a special execution process:
- Write the program in a
.javafile. - Compile the code using the Java compiler.
- The compiler generates bytecode (
.classfile). - The JVM reads the bytecode and executes it.
Java Execution Flow#
Java Code (.java)
↓
Compiler (javac)
↓
Bytecode (.class)
↓
JVM
↓
Output
This execution process is one of the reasons Java is platform independent.
Understanding JVM, JRE, and JDK#
Many beginners hear the terms JVM, JRE, and JDK together and get confused. Let's understand them one by one.
What is JVM?#
JVM stands for Java Virtual Machine.
It is responsible for running Java programs.
The JVM reads the bytecode generated by the compiler and converts it into machine-level instructions that the computer can understand.
Real-Life Analogy#
Imagine writing a letter in English.
People from different countries may speak different languages, but a translator can convert your message into a language they understand.
Similarly:
- Java Code → Letter
- JVM → Translator
- Computer → Reader
Because every operating system has its own JVM implementation, the same Java program can run on Windows, Linux, and macOS.
This is the foundation of Java's Write Once, Run Anywhere principle.
What is JRE?#
JRE stands for Java Runtime Environment.
It provides everything required to run a Java application.
JRE contains:
- JVM
- Supporting Libraries
- Runtime Files
If you only want to run Java programs, JRE is enough.
What is JDK?#
JDK stands for Java Development Kit.
It is used by developers to create Java applications.
JDK contains:
- JRE
- JVM
- Java Compiler (
javac) - Development Tools
If you want to write, compile, and run Java programs, you need the JDK.
Relationship Between JVM, JRE, and JDK#
JDK
└── JRE
└── JVM
You can think of it like this:
- JVM → Runs Java Programs
- JRE → Provides Environment to Run Java Programs
- JDK → Provides Tools to Develop Java Programs
Your First Java Program#
Now that we understand how Java works, let's write our first Java program.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output#
Hello World
Understanding the Program#
Let's break the program into smaller parts.
public class Main#
This creates a class named Main.
The file name should match the class name.
Main.java
public static void main(String[] args)#
This is the entry point of every Java program.
Whenever a Java program starts running, execution begins from the main() method.
For now, simply remember:
This is where the program starts executing.
System.out.println()#
This statement is used to display output on the screen.
System.out.println("Hello World");
The text inside double quotes is printed to the console.
print() vs println()#
Java provides two commonly used methods for displaying output.
Example#
System.out.print("Hello ");
System.out.println("World");
System.out.println("Welcome to Java");
Output#
Hello World
Welcome to Java
Difference Between print() and println()#
| print() | println() |
|---|---|
| Prints output on the same line | Prints output and moves to the next line |
| Does not add a new line automatically | Adds a new line automatically |
Check Your Understanding#
1. What is Java, and why is it called a platform-independent language?
2. Explain the difference between JVM, JRE, and JDK in your own words.
3. What happens when a Java program is compiled and executed? Explain the complete Java execution flow.
4. Is Java a 100% Object-Oriented Programming Language? Research this question and try to justify your answer with an example.
With these fundamentals in place, you're now ready to explore comments, identifiers, keywords, data types, and other core Java concepts.