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
-
Regular expression basics
A summary of the syntax, and the one thing about Java's version that trips everyone.
-
Regular expressions in Java
Two classes do the work, and three String methods hide them for simple cases.
-
Comparing strings
The single most common mistake in beginner Java, and the reason it is so persistent.
-
StringBuilder and StringBuffer
Strings are immutable, so every concatenation makes a new one.
-
Joining an array into a string
Almost always one line.
-
Splitting a string into an array
One method, and three behaviours that surprise people.
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
-
Keyboard input
The obvious approach is the one that does not work, for a reason worth understanding.
-
Common GUI patterns
A handful of arrangements that turn up in every desktop application.
-
Menus
A menu bar, menus, and items. Three classes and a couple of conventions.
-
Null layout, and why not to use it
Setting coordinates by hand. Tempting, and wrong in four specific ways.
-
Alternatives to writing a Swing GUI by hand
Building a Java interface is not hard. Building one that is easy to maintain and does not look dated is the hard part, and several approaches try to solve it.
-
Presentation model
Move the state of the screen out of the screen.
-
Model-View-Controller
Keep what the program knows separate from how it is shown.
-
How the UI and the model talk
Downward by method call, upward by event. Never the other way round.
-
A minimal text editor
Enough of an application to meet every part of a Swing program at once.
-
PaintDemo: a mouse-driven painting program
Event-driven drawing in Swing: mouse listeners, paintComponent, and a BufferedImage that makes the picture persist.