Main.javaCourse
Java
A typed variable, then a class with a method, then null, integer division, a record that keeps cents, and a method list you can call.
- nameStringlatte
- centsint450
- readybooleantrue
6 lessons, in orderFree to read
Syllabus
Basics
The types, explained, then an object you can name and call.
-
01
Java from zero: a variable has a type
2 min read
A Java variable has a type. name is a String, cents is an int, and ready is a boolean. The program prints latte $4.50 ready.
public class Main { public static void main(String[] args) { String name = "latte"; int cents = 450; boolean ready = true; String status = ready ? "ready" : "sold out"; System.out.printf("%s $%d.%02d %s%n", name, cents / 100, cents % 100, status); } }latte $4.50 ready -
02
A class groups fields and a method. Item stores a drink and a price in cents. label prints latte $4.50.
Builds on lesson 1.
public class Main { static class Item { String name; int cents; Item(String name, int cents) { this.name = name; this.cents = cents; } String label() { return String.format("%s $%d.%02d", name, cents / 100, cents % 100); } } public static void main(String[] args) { Item latte = new Item("latte", 450); System.out.println(latte.label()); } }latte $4.50
Practice
A rule that prevents a bug, then a program that uses the object.
-
03
Java from zero: null is not an object
2 min read
A variable can hold null, which means no object. Calling a method on null fails. The program checks first and prints missing.
Builds on lessons 1 through 2.
public class Main { static class Item { String name; int cents; Item(String name, int cents) { this.name = name; this.cents = cents; } String label() { return String.format("%s $%d.%02d", name, cents / 100, cents % 100); } } public static void main(String[] args) { Item item = null; String text = item == null ? "missing" : item.label(); System.out.println(text); } }missing -
04
Dividing two ints drops the fraction. A $5.00 bill split three ways is 166 cents each, not 166.666. The program prints 166. Two cents are left over.
Builds on lessons 1 through 3.
public class Main { public static void main(String[] args) { int cents = 500; int people = 3; System.out.println(cents / people); } }166
Further
One more idea, then a program that puts the pieces together.
-
05
Java from zero: a record keeps the cents
2 min read
A record is a small object with fields the constructor sets. Money holds cents. Adding three lattes of 450 prints 1350.
Builds on lessons 1 through 4.
public class Main { record Money(int cents) { Money add(Money other) { return new Money(Math.addExact(this.cents, other.cents)); } } public static void main(String[] args) { Money latte = new Money(450); Money total = latte.add(latte).add(latte); System.out.println(total.cents()); } }1350 -
06
Java from zero: one method is enough to call
2 min read
An interface is the method list a function needs. Labeller asks for label. Item has label. show prints latte $4.50.
Builds on lessons 1 through 5.
public class Main { interface Labeller { String label(); } static class Item implements Labeller { String name; int cents; Item(String name, int cents) { this.name = name; this.cents = cents; } public String label() { return String.format("%s $%d.%02d", name, cents / 100, cents % 100); } } static void show(Labeller item) { System.out.println(item.label()); } public static void main(String[] args) { show(new Item("latte", 450)); } }latte $4.50
Readings
Shorter pieces in the same language. Any order is fine once the lessons feel familiar.
-
Streams: nothing runs until the end
2 min
GoByte Skills #6: filter and map do not filter or map anything. They write down a recipe, and no element moves until a terminal call drives the pipeline, pushing elements through one at a time. Bonus: why count() can skip your peek.
-
Sealed types: the switch that cannot forget
2 min
GoByte Skills #5: Seal the hierarchy, drop the default, and adding a type turns every switch that missed it into a compile error with a line number.
-
Virtual threads: blocking is cheap again
3 min
GoByte Skills #4: 10,000 tasks that each sleep 1 s finish in about 1.1 s on a 14 core laptop, in plain blocking Java. Virtual threads park and free the carrier. The catch on Java 21: synchronized pins it.