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.

A cup label is three typed values
  1. nameStringlatte
  2. centsint450
  3. readybooleantrue

6 lessons, in orderFree to read

Syllabus

Basics

The types, explained, then an object you can name and call.

  1. 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
    
  2. 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.

  1. 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
    
  2. 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.

  1. 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
    
  2. 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.

  1. 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.

  2. 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.

  3. 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.