Start With the Problem#
StudyBuddy needs to do something simple: for each student, take the number of quiz questions they answered correctly, give 10 points per correct answer, add a 20-point bonus if they finished on time, print a grade, and say who scored highest.
A human does this with a pencil in two minutes. But the computer (How Computers Work Ch 5) is a cook who only follows tiny, exact steps: "put 18 in a box", "multiply the box by 10", "if the box is at least 200, write A". It will do this billions of times per second, perfectly — but only if every step is written exactly, with no ambiguity.
A programming language is how you write those exact steps in a form a human can read and a machine can translate. By the end of this file you'll write that whole StudyBuddy report program yourself (Chapter 9) — and every piece of it will be something you understand.
The Plain Picture#
A recipe card in a cookbook has a fixed layout: the dish name at the top, then "Steps:", then numbered steps. The cook always starts reading at "Steps:". A Java program is the same: a fixed outer shape, and one special place where running begins.
The Real Names#
Java is a programming language released by Sun Microsystems in 1995 (now owned by Oracle). It's one of the most used languages for large backend systems (banks, Amazon, Google, LinkedIn, Uber), Android apps, and — for you — DSA interviews.
Java code is compiled to bytecode and run by the JVM with a JIT compiler (How Computers Work Ch 8).
Three names people mix up:
| Name | What it is | Contains |
|---|---|---|
| JVM (Java Virtual Machine) | the program that runs bytecode | interpreter + JIT + garbage collector |
| JRE (Java Runtime Environment) | everything needed to run Java programs | JVM + the standard library |
| JDK (Java Development Kit) | everything needed to write Java programs | JRE + the compiler javac + tools |
You install the JDK. Modern Java distributions no longer ship a separate JRE.
Versions: a new Java version comes out every 6 months; every 2 years one is an LTS (Long-Term Support) release that companies standardise on: 8, 11, 17, 21, 25. This curriculum uses Java 21.
Under the Hood#
Install and check#
java -version # java version "21.0.8" 2025-07-15 LTS
javac -version # javac 21.0.8If they're missing: brew install openjdk@21 (then follow the message Homebrew prints about linking it), or download a JDK from
adoptium.net.
The first program#
Make a file named exactly Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, StudyBuddy!");
}
}Run it — two ways:
javac Hello.java # 1. compile: creates Hello.class (bytecode)
java Hello # 2. run the class → Hello, StudyBuddy!
java Hello.java # shortcut (Java 11+): compile in memory and run a single file in one stepReading it line by line#
public class Hello { ← a CLASS named Hello. Every Java program lives inside classes.
The file name must match: Hello.java
public static void main(String[] args) { ← the MAIN METHOD: where running starts. Memorise this line.
System.out.println("Hello, StudyBuddy!"); ← one STATEMENT: print a line of text. Ends with ;
} ← end of main
} ← end of classBraces
{ }group lines into a block — "everything inside belongs together".Semicolons
;end each statement, like a full stop ends a sentence.Java is case-sensitive:
Main≠main,String≠string.Comments are notes for humans that the compiler ignores:
Java // a comment until the end of the line /* a comment over several lines */
What the words public, static, void, String[] args mean — each is taught properly later:
void= the method returns nothing (Ch 7);static= belongs to the class itself, so it can run without creating an object (Ch 7 and Classes, Objects and Memory);public= callable from anywhere (Object-Oriented Programming);String[] args= an array of text values passed in when the program starts (Ch 8).
For now: treat public static void main(String[] args) as the fixed "Steps:" heading of the recipe card.
Compile errors vs runtime errors#
A compile error is caught by
javacbefore the program runs — a missing;, a misspelled name, a wrong type. The compiler tells you the file and line:Output Hello.java:3: error: ';' expectedA runtime error (an exception) happens while the program is running — e.g. reading past the end of an array (Ch 8). Java is a statically typed language (Ch 2), so it catches many mistakes at compile time that languages like Python only find while running.
Gotchas
File name must match the public class name.
public class Hellomust be inHello.java, orjavacrefuses.Read the first compile error first. One missing brace can produce 20 errors; fixing the first often fixes the rest.
Check yourself
4 questionsWhat's the difference between the JDK, the JRE and the JVM?
Show answerHide answer
JVM runs bytecode; JRE = JVM + standard library (to run programs); JDK = JRE + compiler and tools (to write programs).
What does javac produce, and what runs it?
Show answerHide answer
A .class file of bytecode; the JVM runs it.
Where does a Java program start running?
Show answerHide answer
In the main method: public static void main(String[] args).
Compile error vs runtime error — give an example of each.
Show answerHide answer
Compile error: missing semicolon (caught by javac). Runtime error: accessing an array index that doesn't exist (happens while
running).