☕ Introduction to Java — What It Is, JDK vs JRE vs JVM
Learn what Java is, how the JVM works, and the difference between JDK, JRE and JVM. Beginner-friendly intro to write-once-run-anywhere with examples and interview Q&A.
Java is a class-based, object-oriented programming language designed to run anywhere. Write your code once, compile it to bytecode, and it runs on any device that has a Java Virtual Machine — Windows, Linux, macOS, Android, servers, smart cards.
🧠 The 'Write Once, Run Anywhere' model
Other languages (C, C++) compile directly to machine code for one specific OS/CPU. Java compiles to bytecode — a portable intermediate format — which the JVM translates to machine code at runtime. One .jar file runs on every platform.
📦 JDK vs JRE vs JVM (the #1 beginner confusion)
- JVM (Java Virtual Machine) — runs the bytecode. The engine.
- JRE (Java Runtime Environment) — JVM + standard libraries. Enough to run Java apps.
- JDK (Java Development Kit) — JRE + compiler (
javac) + tools. Enough to build Java apps.
Mental model: JDK ⊃ JRE ⊃ JVM. To develop, install the JDK.
💡 Why Java still dominates
Java powers most enterprise backends, Android apps, big-data tools (Hadoop, Spark, Kafka) and high-frequency trading systems. It's statically typed, fast (JIT-compiled), and has a 25-year ecosystem. If you're learning programming fundamentals in C++ or coming from JavaScript, Java's strict typing and OOP-first design will feel rigorous but rewarding.
💻 Code Examples
Your first Java program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}Hello, Java!
Compile then run (the two-step model)
// 1. compile source -> bytecode
javac Main.java // produces Main.class
// 2. run bytecode on the JVM
java MainHello, Java!
⚠️ Common Mistakes
- Confusing the JDK, JRE and JVM — install the JDK to develop, not just the JRE.
- Naming the file differently from the public class — `public class Main` must live in `Main.java`.
- Forgetting that `main` must be `public static void main(String[] args)` exactly — the JVM looks for that signature.
- Thinking Java is interpreted only — it's compiled to bytecode, then JIT-compiled to native code at runtime.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is the difference between JDK, JRE and JVM?Beginner
Q2.What does 'write once, run anywhere' mean?Beginner
Q3.Is Java compiled or interpreted?Intermediate
Q4.What is bytecode?Intermediate
Q5.Why is the main method static?Beginner
🧠 Quick Summary
- Java = class-based, object-oriented, statically typed, runs on the JVM.
- Source → javac → bytecode → JVM → native code (JIT).
- JDK ⊃ JRE ⊃ JVM; install the JDK to develop.
- Write once, run anywhere via portable bytecode.
- main must be `public static void main(String[] args)`.