Introduction to Java and the JVM
Learn what Java is, how Java programs are compiled, and how the JVM allows Java code to run on different computers.
Introduction to Java and the JVM
Welcome to Java! 🎯
If you have never written code before, don't worry. We will start from the absolute beginning and build your understanding step by step.
By the end of this lesson, you will understand:
- What Java actually is
- What happens when you write Java code
- What a compiler does
- What the JVM is
- What bytecode means
- Why Java is called "Write Once, Run Anywhere"
What Is Java?
Java is a programming language. A programming language is a way for humans to give instructions to computers.
Think about how you communicate with another person. You might say: "Please open the door."
The other person understands your language and performs the action. A computer, however, does not naturally understand human languages like English or Hindi. It ultimately works with very low-level instructions represented as machine code.
Java gives us a human-friendly way to describe what we want the computer to do. For example:
System.out.println("Hello!");
This means: "Computer, display the word Hello!"
Why Can't the Computer Directly Understand Java?
This is where an important problem appears. Imagine:
- You speak English
- Your friend speaks Japanese
- Neither of you understands the other's language.
You need a translator. Java has a similar problem.
You write:
System.out.println("Hello!");
But your computer's processor does not directly understand Java source code. We therefore need something that translates our Java program into a form that can eventually be executed by the computer.
This brings us to the compiler and the JVM.
The Human Translator Analogy
Imagine you are giving instructions to a person in another country. The process could look like this:
You → Translator → Instructions understood by the other person
Java works somewhat similarly:
Java Source Code → Compiler → Bytecode → JVM → Computer
Let's understand each part.
1. Java Source Code
The code you write is called source code.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello!");
}
}
This is written in a language that humans can reasonably understand. The file usually has a .java extension.
2. The Compiler
A compiler is a program that translates your Java source code into another form that the Java runtime can execute. In Java, the compiler commonly used is called javac.
You can think of the compiler as a translator. It checks your Java code for things such as:
- Syntax errors
- Invalid statements
- Incorrect types
3. Bytecode
After compilation, the compiler produces something called bytecode, stored in a .class file.
Bytecode is an intermediate form of instructions designed to be executed by the Java Virtual Machine.
4. The JVM
JVM stands for Java Virtual Machine. The JVM is a software-based machine that understands Java bytecode and executes it on the actual computer.
Why Do We Need the JVM?
Imagine you want the same program to run on Windows, Linux, and macOS. Each operating system uses different machine instructions. Java solves this by using bytecode. The compiler produces bytecode, and each platform has a JVM capable of executing that bytecode.
Java's "Write Once, Run Anywhere" Idea
The basic idea is:
- Write Java source code.
- Compile it into bytecode.
- Run that bytecode using a JVM appropriate for the platform.
Remember This
If you remember only one thing from this lesson, remember: Java source code is compiled into bytecode, and the JVM executes that bytecode.
Written by
ThinkWisely Team
Have a Question or Suggestion?
We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!