Java Tutorial
Java is one of the most widely used programming languages in the world — powering everything from Android apps and enterprise back-ends to big-data pipelines and cloud-native microservices. Whether you are writing your very first line of code or you want to sharpen your understanding of what the JVM is doing under the hood, this tutorial takes you from zero to confident, step by step.
What is Java?
Java is a compiled, object-oriented, platform-independent programming language created by Sun Microsystems in 1995 (now maintained by Oracle). Its signature promise — write once, run anywhere — comes from compiling source code to bytecode that runs on the Java Virtual Machine (JVM), rather than directly to native machine code. That single design decision is why the same .jar file can run on Windows, macOS, Linux, or an embedded device without recompilation.
// The simplest Java program — a great sanity check after installing the JDK
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Note: Every Java application needs at least one
classwith apublic static void main(String[] args)method. That method is the entry point the JVM looks for when you run your program.
Why learn Java in 2026?
- Massive ecosystem — Maven Central hosts millions of libraries; Spring, Hibernate, Kafka, and Hadoop are all Java-native.
- Strong job market — Java consistently ranks in the top three most in-demand languages for backend, Android, and enterprise roles.
- Stable, long-term language — Java 21 (LTS) brings modern features like virtual threads, records, sealed classes, and pattern matching while keeping backwards compatibility.
- Deep learning value — Java’s explicit type system, OOP model, and JVM memory model teach fundamentals that transfer to every other language.
- Tooling — IntelliJ IDEA, Eclipse, VS Code, JUnit, Maven, and Gradle make the development cycle fast and productive.
How Java works (the big picture)
Understanding the path from source file to running program is worth ten minutes of your time upfront. Here is the journey:
- You write source code in a
.javafile. - The Java compiler (
javac) compiles it to platform-neutral bytecode stored in a.classfile. - The JVM loads the
.classfile, verifies it, and starts executing it — either by interpreting bytecode directly or by letting the JIT compiler translate hot paths to native machine code for maximum speed.
YourCode.java → [javac] → YourCode.class → [JVM + JIT] → Native execution
This architecture is why Java is fast enough for high-throughput servers while remaining portable. Explore the full picture in How a Java Program Runs and JIT Compilation & Bytecode.
Under the Hood
When your program runs, the JVM manages several key subsystems simultaneously:
- Class Loader Subsystem — finds, loads, links, and initializes
.classfiles on demand. See Class Loaders & Class Loading. - Runtime Data Areas — the heap (where objects live), the stack (one per thread, holds stack frames), the method area (class metadata), and the PC register.
- Execution Engine — interprets bytecode initially; the JIT compiler profiles execution and recompiles hot methods to optimized native code, which is why long-running Java processes get faster over time.
- Garbage Collector — automatically reclaims heap memory from unreachable objects, eliminating manual
malloc/freeand most memory leaks. The G1 and ZGC collectors in Java 21 achieve sub-millisecond pause times. See Garbage Collection Deep-Dive.
This design sits between the interpreted flexibility of Python and the raw-metal control of C++. For a direct comparison of where Java’s trade-offs land, see C++ vs Java.
Java versions at a glance
| Version | Released | Key additions |
|---|---|---|
| Java 8 | 2014 | Lambdas, Stream API, Optional, new Date/Time API |
| Java 11 (LTS) | 2018 | var in lambdas, HttpClient, removed EE/Corba modules |
| Java 17 (LTS) | 2021 | Sealed classes, records, pattern matching preview |
| Java 21 (LTS) | 2023 | Virtual threads, sequenced collections, pattern matching GA |
Tip: Stick to a Long-Term Support (LTS) release — Java 21 — for new projects. LTS versions receive security and bug fixes for years, while non-LTS releases are supported for only six months.
Setting up your environment
Before you can compile and run Java programs you need:
- JDK (Java Development Kit) — includes
javac,java,javadoc, and more. Download the latest Java 21 LTS from adoptium.net or your OS package manager. - PATH configuration — so your terminal can find
javaandjavacfrom any directory. Detailed steps are in Setting the PATH. - An IDE or editor — IntelliJ IDEA Community Edition is free and the industry standard for Java development.
Not sure what “JDK” vs “JRE” vs “JVM” means? JDK, JRE & JVM has a clear breakdown.
In This Section
Work through these pages in order if you are new, or jump directly to any topic you need:
- History of Java — How Java evolved from a set-top-box language in 1991 to a global enterprise platform, and why that history still shapes the language today.
- Features of Java — The core properties — platform independence, strong typing, automatic memory management, multi-threading, and more — that make Java what it is.
- C++ vs Java — A practical comparison of memory management, performance trade-offs, pointers, operator overloading, and when to choose each language.
- Your First Java Program — Step-by-step: create, compile, and run
HelloWorld.javafrom the command line and understand every keyword in it. - How a Java Program Runs — The full lifecycle of a
.javafile: compilation to bytecode, class loading, JVM initialization, and execution. - Setting the PATH — Configure your
JAVA_HOMEandPATHenvironment variables on Windows, macOS, and Linux sojavacandjavawork from any terminal. - JDK, JRE & JVM — Understand the difference between the development kit, the runtime environment, and the virtual machine — a question that trips up almost every beginner.
- JVM Architecture — A detailed look at the class loader subsystem, runtime data areas, execution engine, and native method interface.
- Class Loaders & Class Loading — How the bootstrap, extension, and application class loaders work, the delegation model, and how to write a custom class loader.
- JIT Compilation & Bytecode — How the HotSpot JIT compiler profiles your code and compiles hot methods to native code, with tips on understanding and influencing optimizations.
- Garbage Collection Deep-Dive — Generational GC theory, the G1 and ZGC collectors, GC tuning flags, and how to interpret GC logs.
- Variables — Declaring, initializing, and scoping local, instance, and static variables, plus best practices for naming and immutability.
- Data Types — Java’s eight primitive types, their sizes and default values, type casting, and when to reach for wrapper classes instead.
- Unicode System — Why Java uses UTF-16 internally, how
charmaps to Unicode code points, and how to handle characters outside the Basic Multilingual Plane. - Operators — Arithmetic, relational, logical, bitwise, shift, ternary, and assignment operators with precedence rules and gotchas.
- Java Keywords — A reference for all 67 reserved words in Java 21, what each one does, and which ones beginners frequently misuse.
A note on practice
Reading documentation is only half the battle. After each page, type the examples yourself — do not copy-paste. Muscle memory accelerates learning, and you will catch subtleties that passive reading misses. Once you are comfortable with the basics, challenge yourself with the Practice Programs collection.
Related Topics
- OOP Concepts — Once you know the syntax, object-oriented design is the next big leap; start here.
- Control Statements — if/else, switch, loops — the building blocks of any algorithm.
- Exception Handling — Writing robust programs means handling errors gracefully from day one.
- Collections Framework — Java’s rich library of lists, sets, maps, and queues you will use in virtually every project.
- Java 8 Features — Lambdas and the Stream API changed how Java code is written; essential reading after the basics.
- Java 21 LTS Features — Virtual threads, records, sealed classes — the modern Java you should be writing today.