Part 04:
1. Records
Think of a record as a data carrier class without boilerplate.
● Traditionally, Java classes for data objects needed constructors, getters, toString(), equals(),
hashCode().
● Records give you all of this automatically.
● Great for DTOs (Data Transfer Objects), responses in APIs, immutable configs.
Key points:
● Immutable by default.
● Compact syntax.
● Best for modeling data, not behavior.
2. Switch Expressions
The old switch was clunky. Java 17 makes it expression-based:
● You can assign a value directly from a switch.
● No more fall-through errors unless you want them.
● Cleaner, more expressive code.
Use cases: Mapping enums to values, routing logic based on type or status codes.
3. Sealed Classes
A way to control inheritance.
● A sealed class restricts which classes can extend or implement it.
● Useful for defining a closed hierarchy (e.g., shapes: only Circle, Rectangle, Triangle allowed).
● Improves type safety and enables better compiler checks.
Compare it to final (no subclassing at all) and abstract (anyone can subclass). Sealed sits in the middle.
4. Pattern Matching for instanceof
● Before: check type with instanceof, then cast manually.
● Now: you can both check and bind the variable in one go.
● Safer, less verbose, more readable.
5. Text Blocks (from Java 15, but standard in 17 LTS)
● Multi-line strings using triple quotes.
● Perfect for JSON, SQL queries, HTML templates in code.
● No messy concatenation or escaping.
6. Other niceties
● var for local variables → type inference.
● Helpful NullPointerExceptions → tells you which variable was null.
● Improved garbage collectors → more efficient memory handling.
● JEPs like foreign function & memory API (preview) → interop with native code without JNI.
JEP = how new Java features are proposed and added.
JNI = the old-school bridge between Java and native code.
Backend Relevance
● Records → DTOs for API responses, request objects, database row mappings.
● Switch expressions → cleaner routing logic, mapping HTTP status codes, handling enums.
● Sealed classes → modeling domain hierarchies (e.g., payment methods, user roles).
● Pattern matching → safer request validation and object checks.
● Text blocks → SQL queries, JSON templates, config scripts in code.