Modern Java You'll Use in Spring
Spring code uses some newer Java features. Here's a quick refresher - every example runs with plain Java 17+.
Spring code relies on modern Java: records for request and response data, lambdas and streams for working with lists, and Optional for values that may be missing. Being comfortable with these makes Spring examples much easier to read.
Records: small data classes
Example
A record gives you the constructor, getters, equals and toString for free. Spring turns records into JSON automatically - perfect for API requests and replies.
Streams and lambdas
Example
Optional: a value that may be missing
Example
Spring Data's findById returns an Optional, so you'll use these methods a lot.
var and text blocks
Example
Key points
- Records are compact data classes.
- Streams and lambdas process collections.
- Optional represents maybe-missing values.
📝 Test yourself
1. What does a record give you automatically?
2. What does Optional represent?
3. Fill in the keyword for a compact data class:
Product(String name, int price) {}
