LeepointJava Reference

Java Notes

The main body of notes, organised by subject.

Each page covers one topic: what the language guarantees, the smallest example that makes it clear, and the mistakes that are common enough to be worth naming.

Where behaviour changed between Java versions, the page says which version and what changed. Examples are written against a current LTS release.

Strings and text

Arrays and collections

  • Reversing an array

    Two indices walking toward each other. The whole exercise is where they stop.

  • Comparators

    Comparable is how a class sorts itself. Comparator is every other order you might want.

  • Selection sort

    Find the smallest, put it first, repeat. The simplest sort to reason about.

  • Sorting arrays

    Use the library. It is better than what you would write, for reasons worth knowing.

  • Two-dimensional arrays

    Java has no true 2D array. It has arrays whose elements are arrays.

  • Arrays

    Fixed length, decided when you create it, and never changing.

  • ArrayList

    A growable array. The default choice for a list, and usually the right one.

Numbers and operators

  • Comparing objects

    == asks whether two variables point at the same thing. Usually you meant something else.

  • Bitwise operators

    Operating on individual bits. Rarer than it used to be, still occasionally exactly right.

  • BigInteger

    Integers with no size limit, at the cost of every operation being a method call.

Control flow

  • Braces on if statements

    Optional for a single statement. Use them anyway.

  • Exceptions

    A separate channel for reporting that something went wrong, so the normal path stays readable.

  • The for loop

    Three expressions, any of which may be empty.

  • The for-each loop

    Use it whenever you can, which is most of the time.

  • Static methods

    A method that belongs to the class rather than to any object of it.

  • switch

    Two forms now: the old one with break, and the arrow one without.

Classes and objects

  • import

    A shorthand for names. It does not load or include anything.

  • Calling the superclass constructor

    The parent is always constructed first, whether or not you say so.

  • Constructors

    Runs once, when the object is created, to put it into a valid state.

  • Fail early

    A bug found at its cause is cheap. The same bug found three layers away is not.

Algorithms

  • Big-O notation

    How the cost grows as the input grows. Not how long anything takes.

  • Random numbers

    Pseudo-random: deterministic given the seed, which is a feature.

  • Binary search

    Halve the range each step. Requires sorted data, and that requirement is the whole cost.

Files, time and system

  • Platform-independent newlines

    Windows uses two characters, Unix one. Mostly you should let the library decide.

  • System properties

    A map of strings the JVM keeps about its environment.

  • Timers

    Three classes, two of them called Timer, and the difference matters.

  • Dates and times

    Use java.time. The older classes are still there and should not be.

Swing and GUI