← All lessons
Java
All Java snippets · 32
Every snippet has its own focus page with an explanation and the output it produces.
Variables & Types · 3
- Primitive TypesPrimitives hold values directly rather than references. They cannot be null and have fixed sizes defined by the language.
- Local Type Inferencevar infers the type from the initialiser. It works only for local variables, never for fields or method parameters.
- Integer CachingJava caches boxed integers from -128 to 127, so == accidentally works in that range. Always compare wrapper objects with equals.
Strings · 3
- Strings Are ImmutableEvery String method returns a new object rather than modifying the original, so the result must be assigned to be kept.
- Compare Strings With equals== compares references. Literals share an interned instance, but a new String allocates a separate object, so only equals is reliable.
- Building Strings EfficientlyConcatenating with + inside a loop allocates a new String each pass. StringBuilder mutates one buffer instead.
Control Flow · 3
- Enhanced For LoopThe for-each form iterates arrays and anything implementing Iterable without an index variable to get wrong.
- Switch ExpressionsThe arrow form returns a value and never falls through, removing the missing-break bug that plagued the old switch.
- Ternary OperatorThe conditional operator picks between two expressions. Both branches must produce compatible types.
Arrays · 2
Collections · 3
- ArrayList BasicsDeclare against the List interface and instantiate a concrete class, so the implementation can change without touching callers.
- HashMap And getOrDefaultmerge inserts the value or combines it with the existing one, replacing the usual containsKey check and manual increment.
- List.of Is UnmodifiableList.of returns an unmodifiable list. Any mutation throws at runtime rather than being caught by the compiler.
Classes · 3
- Class With Constructorthis distinguishes the field from the parameter of the same name. final fields must be assigned exactly once, in the constructor.
- Records Remove BoilerplateA record generates the constructor, accessors, equals, hashCode and toString from its component list. Instances are immutable.
- Static MembersA static field belongs to the class rather than any instance, so every object shares the same variable.
Inheritance · 3
- Overriding MethodsThe method that runs is chosen by the object's real type, not the reference type. @Override makes the compiler verify the signature.
- Interface Default MethodsA default method supplies an implementation on the interface itself. An interface with one abstract method can be written as a lambda.
- Abstract ClassesAn abstract class cannot be instantiated and may mix abstract methods with concrete ones that build on them.
Exceptions · 3
- Catching Exceptionsfinally executes whether or not an exception was thrown, which makes it the place for cleanup.
- Checked vs UncheckedExtending RuntimeException makes an exception unchecked, so callers are not forced to declare or catch it.
- Try With ResourcesAnything implementing AutoCloseable is closed automatically at the end of the block, even if an exception escapes.
Generics · 2
Streams · 3
- Stream PipelineIntermediate operations are lazy and only run when a terminal operation such as toList or collect is reached.
- Grouping With CollectorsgroupingBy builds a map from a classifier function, replacing the usual loop that creates lists on demand.
- Streams Are Single UseA stream can be traversed once. Consuming it twice throws, so build a fresh stream from the source instead.