Beginner⏱️ 7 min📘 Topic 1 of 22

☕ 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!");
  }
}
Output:
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 Main
Output:
Hello, 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
JVM runs bytecode. JRE = JVM + core libraries (run apps). JDK = JRE + compiler and dev tools (build apps). JDK ⊃ JRE ⊃ JVM.
Q2.What does 'write once, run anywhere' mean?Beginner
Java compiles to platform-independent bytecode. Any device with a compatible JVM can run that same bytecode, so you don't recompile per operating system.
Q3.Is Java compiled or interpreted?Intermediate
Both. javac compiles source to bytecode (compilation). At runtime the JVM interprets bytecode and the JIT compiler hot-compiles frequently-used paths to native machine code (compilation again) for speed.
Q4.What is bytecode?Intermediate
An intermediate, platform-independent instruction set stored in .class files. It's not machine code — the JVM translates it to native instructions for the host CPU at runtime.
Q5.Why is the main method static?Beginner
The JVM must call main without creating an object of the class first. static lets it be invoked on the class directly, before any instance exists.

🧠 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)`.