🎯 What you will learn
Java powers over 3 billion devices — Android apps, bank transaction engines, Spring Boot microservices, and increasingly, AI inference gateways. Before any of that, you need one working terminal command:
The two commands you will use every single day:
javac MyFile.java — compile source to bytecode
java MyFile — run that bytecode on the JVM
This lesson installs the right version (JDK 21 LTS), runs your first program, and explains the compile → run lifecycle so you never stare at a cannot find symbol error without knowing why.
🔧 JDK vs JRE vs JVM — the three layers
Most beginners install the wrong thing. Here is the exact hierarchy:
| Term | Contains | Used by |
|---|---|---|
| JVM | Runtime engine — executes .class bytecode | Everyone who runs Java |
| JRE | JVM + standard class libraries | End users running Java apps |
| JDK | JRE + javac compiler + dev tools | Developers writing Java code ← you need this |
Why JDK 21? It is the current Long-Term Support (LTS) release — meaning security patches until at least 2031. Companies hiring Java developers today target JDK 11 or JDK 21. We go straight to 21.
📦 Installing JDK 21
macOS (Homebrew — recommended)
Windows
Download the installer from adoptium.net → choose Temurin 21 (LTS). The installer sets your PATH automatically. Open a new Command Prompt and run javac -version.
Linux (Ubuntu/Debian)
VS Code setup (optional but recommended): Install the Extension Pack for Java from Microsoft. It adds syntax highlighting, inline errors, and a run button — but the terminal commands are what matter.
💻 Step-by-step: your first program
Open a terminal, create a folder called java-practice, and create a file called HelloWorld.java. The filename must match the class name exactly — Java is case-sensitive.
Now let Java tell you about itself. This program reads system properties — metadata the JVM exposes at runtime.
What just happened?
javac HelloWorld.javacompiled your source intoHelloWorld.class— bytecode, not machine code.java HelloWorldlaunched the JVM, which loadedHelloWorld.classand executedmain().- The JVM's JIT compiler translated hot bytecode paths into native CPU instructions on the fly.
🏦 Real-world scenario
Arjun is a junior backend engineer who just joined SouthBank India's Mumbai office. His tech lead asks him to write a simple program to print a loan application receipt before the real Spring Boot service is built. The team uses Java 21 and Maven, but for this prototype, plain javac is fine. Here is what Arjun writes:
✨ Modern Java 21 features you get for free
Java 21 ships with text blocks (multiline strings), records, pattern matching, and sealed classes. You will use all of these in later lessons. Here is a taste — text blocks that make JSON in Java readable:
🧩 Mini challenge
Create Greeting.java. Print your name, your city, and the current Java version (use System.getProperty("java.version")). Compile and run it.
Use System.out.printf to print a salary figure with a ₹ symbol and comma formatting, e.g. ₹1,200,000. Hint: format string is "₹%,d%n".
Write a text block that contains a two-field JSON object representing a mobile wallet top-up. Print it. Verify the line count with .lines().count().
🎤 Interview Q&A
JVM (Java Virtual Machine) is the runtime engine that executes Java bytecode. It provides memory management, garbage collection, and platform abstraction. JRE (Java Runtime Environment) bundles the JVM with the standard class libraries needed to run compiled Java applications. JDK (Java Development Kit) bundles the JRE with the javac compiler, jar packager, jshell REPL, and other developer tools. Developers need the JDK; end users running pre-compiled apps only need the JRE.
javac compiles .java source files into .class files containing Java bytecode — an intermediate instruction set that is platform-neutral. java launches the JVM, which loads those .class files and either interprets bytecode directly or JIT-compiles hot paths into native machine code for the current CPU architecture. This "write once, run anywhere" model is why the same .class file runs on Windows, Linux, and macOS without recompilation.
The Java Language Specification requires that a file containing a public class be named exactly as that class (case-sensitive, .java extension). The compiler uses this rule to locate source files during compilation and to generate the corresponding .class file. The class loader later locates classes on the classpath using the same fully-qualified name convention. A mismatch causes a compile-time error: class X is public, should be declared in a file named X.java.
The JIT (Just-In-Time) compiler is part of the JVM. Rather than interpreting every bytecode instruction one-by-one, the JIT profiles running code and compiles frequently-executed "hot" methods into native machine code at runtime. This means a Java program starts up quickly (interpreted) and then gets faster as it runs (native-compiled hot paths). Modern JVMs (like HotSpot in JDK 21) are competitive with C++ for throughput workloads because of this adaptive compilation strategy.
Java 21 is the latest LTS release (security patches until 2031+) and adds several production-ready features: Virtual Threads (Project Loom — millions of lightweight threads, critical for high-throughput REST APIs), Record Patterns and Pattern Matching for switch (cleaner data-handling code), Sequenced Collections (consistent first/last access on List/Set/Map), and String Templates (preview). For AI gateway microservices, virtual threads alone justify the upgrade: a Spring Boot service on Java 21 can handle significantly more concurrent LLM calls with the same heap size as Java 17 using platform threads.
📝 Quiz — 5 questions
javac produce?BankApp, what must the source file be named?Strong foundation → move to Lesson 01 and start writing real Java code.
⚠️ Common mistakes
HelloWorld but your file is helloworld.java, javac will refuse. Java filenames are case-sensitive on Linux/Mac — always match the public class name exactly.javac HelloWorld.class fails — javac takes source (.java), not bytecode. java takes the class name without extension: java HelloWorld, not java HelloWorld.class.java but not javac. The compiler lives in the JDK. Always install the JDK (Development Kit), not just the runtime.javac -version shows the wrong one. Run which javac to see which binary is active, and update PATH to prefer JDK 21.✅ Key takeaways
- The JDK contains the compiler (javac) + runtime (JVM) + libraries — always install the JDK for development.
javac File.javaproducesFile.classbytecode;java Fileruns it on the JVM — two separate steps.- The public class name must match the filename exactly (case-sensitive) — Java enforces this at compile time.
- Java 21 LTS is the correct version for new projects — virtual threads, records, text blocks, and pattern matching are all production-ready.
- Java is "write once, run anywhere" because bytecode runs on any JVM, not on a specific CPU or OS.
- The JIT compiler profiles hot paths at runtime and compiles them to native code — Java gets faster as it runs, not slower.
From javac to production Spring Boot
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →