Explain it simply
Java in plain words
Java is a programming language for telling a computer what to do. You write English-like code in a `.java` file. The Java compiler (`javac`) checks that file first and turns it into an in-between form called bytecode.
The Java Virtual Machine (JVM) runs that bytecode. There is a separate JVM for Windows, macOS and Linux, so the same bytecode runs on all of them. Java behaves like a strict teacher: if you forget a semicolon or put a number where text belongs, it makes you fix it before the program is allowed to run.
Problem → Why → Solution
The problem: mixing up two very different kinds of error
Almost every Java beginner writes something like the code below at some point. The lesson count is stored as the text `"35"`, and the semicolon after `println` is missing. When you try to run it, no output appears, only red error messages.
At first it is tempting to read those messages as 'the program crashed' and panic. It did not crash; it never even started. These errors were caught by `javac`, with line numbers, before anything was allowed to run.
public class Main {
public static void main(String[] args) {
int lessonsLeft = "35"; // text stored in a number variable
System.out.println(lessonsLeft) // missing semicolon
}
}
// javac: error: ';' expected
// (after fixing that) error: incompatible types: String cannot be converted to intWhy the difference matters
Compile errors are caught by `javac` before the program runs, so they never reach a user; they are help, not failure. A runtime exception happens while the program is running, which means a user can see it. If you first ask 'did this fail at compile time or at run time?', you find the place to fix much faster.
Core concept: from source file to running program
Rather than memorizing Java as a list of syntax rules, it pays to understand the path a line of code travels before it runs. That path has four stages.
You write source
`Main.java` is a plain text file people can read. It holds classes, methods and statements.
javac checks and compiles
The compiler checks syntax and types. If something is wrong it stops here with a compile error; otherwise it writes `Main.class`, a bytecode file.
The JVM runs the bytecode
The JVM finds the `main` method and starts at its first statement. It begins by interpreting bytecode, and a JIT (Just-In-Time) compiler turns frequently run code into native machine code, so long-running programs get faster.
The operating system does the rest
Printing to the screen, reading files and getting memory are all requests the JVM makes to the operating system.
The Java pipeline
-----------------
Main.java --javac--> Main.class --java--> JVM ---> output
(source) compile (bytecode) run |
errors +-- runtime exceptions
stop here happen hereYou can run every example in this course with a single command, `java Main.java`. Since JDK 11 the `java` launcher can compile one source file in memory and run it straight away, but the compile stage still happens behind the scenes.
In Java, a garbage collector automatically reclaims the memory of objects you no longer use. Closing resources such as files and network connections, and testing your own code, are still the developer's job.
Because Java is statically typed, the compile stage does a lot of work. Every variable and every method parameter has a type, and `javac` checks that those types fit together across the whole program before it runs. That is why most mistakes, such as a misspelled variable name, a value of the wrong type or an unclosed brace, show up while you are still writing code.
The remaining mistakes depend on data and can only be discovered at run time, for example dividing by zero or opening a file that does not exist. The JVM reports those as exceptions, and the Exceptions lesson shows how to handle them.
- Write Main.java
- javac checks and compiles
- Bytecode lands in Main.class
- The JVM runs it on any OS
Whenever you see an error, first decide which stage of this path produced it: compiling or running.
General pattern: the smallest Java program
public class ClassName {
public static void main(String[] args) {
Type name = value; // store a value
System.out.println(name); // show it on the console
}
}All Java code lives inside a class. The JVM looks for the `main` method and runs the statements inside it one by one, top to bottom. Every statement ends with a semicolon (`;`), and curly braces (`{ }`) mark where a block starts and ends.
Your first small Java program
The program below stores the course name and lesson counts in variables, then works out how many lessons are left. Save it as `Main.java` and run it with `java Main.java`. Before you run it, predict the four lines of output.
public class Main {
public static void main(String[] args) {
String course = "Java";
int totalLessons = 36;
int finished = 1;
System.out.println("Welcome to the " + course + " course!");
System.out.println("Lessons finished: " + finished);
System.out.println("Lessons left: " + (totalLessons - finished));
System.out.println("Next lesson: Java Intro");
}
}Code breakdown
- The class that holds everything
public class Main {In Java all code sits inside a class. This one is called `Main`, matching the file name `Main.java`.
- The entry point
public static void main(String[] args) {When the program starts, the JVM looks for a method with exactly this signature. The Syntax lesson explains each keyword in detail.
- Typed variables
int totalLessons = 36;`int` is the type for whole numbers. Every Java variable has a type, and the compiler rejects values that do not fit it.
- Joining text and numbers
System.out.println("Lessons left: " + (totalLessons - finished));The subtraction inside the parentheses happens first, giving `35`, and only then is it joined to the text. Without the parentheses, `"Lessons left: " + 36` becomes a String, and subtracting `1` from a String is a compile error.
Welcome to the Java course!
Lessons finished: 1
Lessons left: 35
Next lesson: Java Intro- Save Main.java
- The compiler checks syntax and types
- The JVM calls main
- Four println calls print their lines
Code is valid
Bytecode is produced and the four lines are printed
Syntax or type error
Compile error with file name and line number; nothing runs
How this course is organized
The course has 36 lessons in five parts. Each part uses what the earlier parts taught, so work through them in order: to follow inheritance in Advanced, for example, you need the classes and constructors from Intermediate.
| Part | What you learn |
|---|---|
| Basic | Java and the JVM, setup, syntax, variables, data types, operators, strings, if/else, switch, loops |
| Intermediate | Arrays, methods, overloading, an OOP intro, classes and objects, constructors, modifiers |
| Advanced | Inheritance, polymorphism, abstraction, encapsulation, interfaces, generics, exceptions, file I/O, ArrayList, HashMap, lambdas and streams, virtual threads, a mini project |
| Projects | Building a small Student Manager app in three parts |
| Exercises | Two practice sets covering fundamentals, OOP, collections and exceptions |
Every lesson follows the same order: a plain explanation, a problem, the core concept, the general syntax pattern, a worked example explained line by line, common mistakes, a mini challenge and three quiz questions. Type the code yourself and run it; reading alone will not teach you Java. If you have no JDK yet, an online compiler such as Replit or OnlineGDB is fine, and the Setup lesson shows how to install one on your own machine.
In real projects
Where Java is used for real
Java has been around since 1995 and is still one of the most widely used languages. Many backend systems at banks, insurers and large e-commerce sites are written in Java with frameworks such as Spring Boot. Android apps can be written in Java, and data tools like Kafka and Elasticsearch, as well as IDEs like IntelliJ IDEA, run on the JVM.
One reason large organizations pick Java is backward compatibility: most code compiled a decade ago still runs on a new JVM. Static types also make big codebases with hundreds of developers safer to change, because when you alter a method, the compiler lists every place that is affected. The basics in this course are exactly what those systems use every day.
Common mistakes
Common beginner mistakes
Reading only the last line of the error list
When the compiler prints many errors, the first one is usually the real cause and the rest follow from it. A single missing brace can produce three or four messages. Look at the file name and line number of the first error, fix that one, then compile again.
Mixing code written for different Java versions
Java code on the internet ranges from Java 8 to the newest release. You might copy a short form where `void main()` appears with no class around it, which only newer JDKs accept, and it will not compile on Java 21. Check your version with `java -version` when you copy code, and stick to the full class form this course uses.
Running without predicting first
Write down the output you expect before you run anything. Every gap between your prediction and the real output points to something you have not understood yet.
Mini challenge
Mini challenge: your own study plan
Write a program that stores your name, the total of 36 lessons and how many lessons you plan to do per week, then prints how many weeks the course will take.
- Use `String` for the name and `int` for the counts
- Work out the weeks with `/` (4 lessons a week gives 9 weeks)
- Print three lines: learner, lessons per week, weeks needed
- Write down the expected output before you run it
public class Main {
public static void main(String[] args) {
// TODO: a String variable for your name
// TODO: an int for the 36 lessons and an int for lessons per week
// TODO: an int for the number of weeks (use /)
// TODO: print "Learner: ...", "Lessons per week: ..." and "Weeks needed: ..."
}
}Expected: With 4 lessons a week it prints three lines: `Learner: Su Su`, `Lessons per week: 4` and `Weeks needed: 9`.
Show hint
Write something like `int weeks = totalLessons / lessonsPerWeek;` and join text and numbers with `+`.
Show solution
public class Main {
public static void main(String[] args) {
String learner = "Su Su";
int totalLessons = 36;
int lessonsPerWeek = 4;
int weeks = totalLessons / lessonsPerWeek;
System.out.println("Learner: " + learner);
System.out.println("Lessons per week: " + lessonsPerWeek);
System.out.println("Weeks needed: " + weeks);
}
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Java source (`.java`) is compiled by `javac` into bytecode (`.class`), which the JVM runs
- Compile errors happen before the program runs; runtime exceptions happen while it runs
- Every program needs a class and a `public static void main(String[] args)` method
- Study the parts in order (Basic → Intermediate → Advanced → Projects → Exercises) and run every example yourself
Java Language Changes, Java SE 25 — Oracle