AheadKit

Free chapter — read online

Java for Everyone: Chapter 1, free to read

This is a free chapter of Java for Everyone: Chapter 1 — “Introduction” — all 6 pages (pages 5–10 of the full 454-page book), exactly as they appear in the book. No edits, no teaser cuts.

Chapter 1: Introduction

Version: Java 21 (LTS)

What Is Java?

In 1995, a team at Sun Microsystems led by James Gosling released a new programming language. It was first called Oak, then renamed Java — named, so the story goes, after the coffee the team drank all day.

Java was built around one promise: Write Once, Run Anywhere. You write your program one time, and it runs on Windows, Mac, and Linux without changes. Back then that was a big deal. Programs usually had to be rebuilt for every operating system.

Thirty years later, Java is still everywhere:

  • Android apps — Java is one of Android's two main languages
  • Banks and large companies — account systems, payment processing, insurance software
  • Web backends — the Spring framework runs a large share of the world's business APIs
  • Big data — Hadoop, Kafka, and much of the data world is built on Java's platform, the JVM

Why Learn Java?

  • Jobs. Java has sat near the top of job postings for decades. Android development, enterprise backends,

banking systems — these employers hire Java developers every year, and the language changes slowly, so what you learn stays useful.

  • It runs almost anywhere. Your compiled Java program is not tied to one operating system. The same file

runs on a Windows laptop, a Linux server, and a Mac, because each machine has its own JVM to run it (more on this below).

  • It teaches you real programming. Java makes you declare types and structure your code in classes. That

feels strict at first, but it builds habits that make every other language easier afterwards.

Java Compared to Python

Here is "Hello!" in Python:

# Python
print("Hello!")

And the same job in Java:

public class Hello {
public static void main(String[] args) {
System.out.println("Hello!");
}
}

Yes, Java asks for more words before anything happens. In exchange you get type safety and a structure that holds up when a program grows from ten lines to ten thousand. Those extra lines are not decoration — the next chapter takes them apart one by one.

Three Names to Know: JDK, JRE, JVM

These three get mixed up constantly, so let's settle them now:

Name What it is

JDK The toolbox you install to write Java programs. It includes the compiler, javac .

JRE The runtime for people who only run Java programs.

JVM The engine inside the JRE that actually executes your compiled code.

The three names, nested — the JDK contains the JRE, and the JRE contains the JVM:

Here is how a program travels from your keyboard to the screen:

  • You write source code in a file like HelloWorld.java .
  • javac compiles it into bytecode, saved as HelloWorld.class .
  • The JVM runs the bytecode, translating it for the real machine as it goes.

That middle step is the whole trick. A C++ compiler produces a file that only Windows can run, or only Linux. Java produces bytecode, and the local JVM — Windows, Mac, or Linux — handles the rest. Write once, run anywhere.

The journey from source code to a running program:

Setup

Install the JDK, version 21 (21 is a long-term support release, so it will be around for years). You can get it free from Adoptium or from Oracle. Then open a terminal and check that the compiler is there:

javac -version
javac 21.0.12.1
java -version
openjdk version "21.0.12.1" 2026-08-18
OpenJDK Runtime Environment (build 21.0.12.1+1-1-24.04.4-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-24.04.4-Ubuntu, mixed mode, sharing)

Your version numbers may differ a little, and the last two lines describe the machine the check ran on. What matters is that javac answers at all — if the terminal says the command was not found, the JDK is not installed or not on your PATH yet.

For an editor, IntelliJ IDEA Community (free) is the most popular choice for Java. VS Code with the Extension Pack for Java also works well.

Your First Program

Create a file named HelloWorld.java and type this into it:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("I am learning Java!");
}
}

Compile it, then run it:

javac HelloWorld.java
java HelloWorld

Output:

I am learning Java!

Two things to notice. First, after compiling, a new file appeared next to your source: HelloWorld.class . That is the bytecode the JVM runs. Second, the run command is java HelloWorld — the class name, with no .class at the end.

Line by Line

Line What it does

public class HelloWorld { Everything in Java lives inside a class. This one is named HelloWorld .

public static void main(String[] args) The starting point. The JVM looks for exactly this method and runs it

{

first.

System.out.println("I am learning Prints the text inside the quotes to the screen, then moves to a new

Java!");

line.

}

Closes the method, and the second } closes the class. Every { needs its matching } .

Don't worry about memorizing public static void main yet. You will type it so often in this book that it will stick on its own.

The Mistake Every Beginner Makes

Sooner or later, everyone hits this one. Say your file is named HelloWorld.java , but inside it you write:

public class Hello {
public static void main(String[] args) {
System.out.println("I am learning Java!");
}
}

The compiler refuses:

HelloWorld.java:1: error: class Hello is public, should be declared in a file named
Hello.java
public class Hello {
^
1 error

Read error messages — they usually tell you exactly what is wrong. This one says it plainly: a public class must live in a file with exactly its own name, plus .java . Class Hello needs Hello.java . Class

HelloWorld needs HelloWorld.java .

GOTCHA Class name = file name, letter for letter. And Java is case-sensitive: HelloWorld , helloworld , and Helloworld are three different names as far as Java is concerned.

A Shortcut for Quick Experiments

Since Java 11, you can skip the separate compile step for a single file and run the source directly:

java HelloWorld.java
I am learning Java!

PRO TIP This shortcut is great for tiny practice programs. For real projects, keep the two-step javac then java habit — that is how build tools run your code, and compiling is the step where whole classes of mistakes get caught early.

Try It Yourself

  • Write a program that prints your name.

Answer

public class Name {
public static void main(String[] args) {
System.out.println("My name is Salam");
}
}
  • Print two sentences, each on its own line. (Hint: call println twice.)

Answer

public class TwoLines {
public static void main(String[] args) {
System.out.println("I am learning Java");
System.out.println("Java is fun");
}
}
  • Find the mistake:
public class HelloWorld {
public static void main(String[] args) {
system.out.println("Hello!");
}
}

Answer

That was Chapter 1. The book has 31 more chapters.

The full book: 454 pages, 32 chapters and 10 step-by-step projects, for $3.99 — or all 11 programming books in the bundle for $12.99.

Added to cart