⚖️ The Short Answer Link to heading
- In Kotlin code, use a data class. It does everything the other two do, and Kotlin doesn’t need Lombok.
- In Java, use a record for immutable value types: request and response bodies, events, results. Every Spring Boot 4 project can, because Spring Boot 4 needs Java 17 or later.
- Use Lombok only where records fall short: a builder for a type with many optional fields, copies with one field changed, or a class that has to be mutable, such as a JPA entity.
- For a type that both languages use, a Java record, or a Kotlin data class marked
@JvmRecord, works cleanly from both sides.
The rest of this post shows why, with the same class written three ways.
🧱 The Same Class, Three Ways Link to heading
A customer with a required id and name, an optional email, and a tier that defaults to FREE.
Kotlin data class:
data class Customer(
val id: String,
val name: String,
val email: String? = null,
val tier: Tier = Tier.FREE,
)
val ada = Customer(id = "c1", name = "Ada")
val pro = ada.copy(tier = Tier.PRO)
Java record:
public record Customer(String id, String name, String email, Tier tier) {
public Customer {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(name, "name");
if (tier == null) tier = Tier.FREE;
}
}
var ada = new Customer("c1", "Ada", null, null);
var pro = new Customer(ada.id(), ada.name(), ada.email(), Tier.PRO);
Lombok @Value and @Builder:
@Value
@Builder(toBuilder = true)
public class Customer {
@NonNull String id;
@NonNull String name;
String email;
@Builder.Default Tier tier = Tier.FREE;
}
var ada = Customer.builder().id("c1").name("Ada").build();
var pro = ada.toBuilder().tier(Tier.PRO).build();
The Kotlin version is the shortest, and its required fields, optional fields and default are all checked by the compiler. The record is close, but the null checks and the default are code you write, and every caller passes all four values in order. Lombok gets the builder and the copy, at the cost of an annotation processor and code you can’t see.
📋 What Each One Generates Link to heading
| Kotlin data class | Java record | Lombok @Value + @Builder |
|
|---|---|---|---|
equals and hashCode |
From the primary constructor’s properties | From the components | From the fields |
Arrays in equals |
By reference | By reference | By content |
toString() |
Customer(id=c1, name=Ada, …) |
Customer[id=c1, name=Ada, …] |
Customer(id=c1, name=Ada, …) |
| Reading a field | customer.name |
customer.name() |
customer.getName() |
| Copy with one change | copy(tier = …) |
None: call the constructor again | toBuilder(), or @With for withTier(…) |
| Default values | Default arguments | Code in the compact constructor | @Builder.Default |
| Named arguments | Yes | No | Builder methods |
| Null safety | Checked by the compiler | Checks you write | Runtime checks from @NonNull |
| Required fields enforced | By the compiler | By the constructor’s argument list | At build(), at runtime, and only with @NonNull |
| Inheritance | Final; can extend an open class and implement interfaces | Final; interfaces only | Final; can extend a class |
| What it needs | The Kotlin compiler | Nothing: it’s the language | An annotation processor, an IDE plugin, and a Lombok release for each new JDK |
Java has a proposal for copying a record with changes, the with expressions in JEP 468. It’s still a candidate, so it isn’t in any release yet.
🛠️ Where Lombok Still Earns Its Place Link to heading
Records removed most of the reasons Java codebases added Lombok. Getters, equals, hashCode and toString for an immutable type now come with the language. What records still don’t do:
- Builders. A record with eight fields, five of them optional, is painful to construct.
@Builderfixes that. - Copies with changes.
@Withgenerates awithTier(…)method per field, andtoBuilder()copies everything into a builder. - Mutable classes. A record is always immutable. JPA entities, and classes that frameworks fill in through setters, still need getters and setters, and
@Getterand@Setterwrite them.
Lombok also works on records. Since version 1.18.20, its features work with records: @NonNull on a record component adds a null check to the compact constructor, and @Builder gives a record a builder. That combination is often the best of both: the record gives you a plain, language-level type, and Lombok adds only the builder.
The costs are real, though:
- A builder doesn’t enforce required fields. In Lombok’s own words, a field “never set during a build session” gets 0,
nullorfalse. Forget.name("Ada")and you get a customer with no name, unless the field is@NonNull, and then you get aNullPointerExceptionatbuild(). Either way it’s a runtime failure, where the Kotlin compiler would have refused the code. - It hooks into the compiler. Lombok works through the Java compiler’s internal APIs, so each new JDK usually needs a new Lombok release. Lombok 1.18.46 added JDK 26 support in April 2026, and 1.18.48 added JDK 27 in September. Upgrading the JDK means checking Lombok first.
- The code isn’t in the source. Debuggers, code review and some tools see methods that aren’t in the file. Most IDEs handle it with a plugin, but it’s one more thing a new developer has to learn.
🪤 Traps to Watch For Link to heading
- Arrays in data classes and records compare by reference. Two customers holding equal
ByteArrayorbyte[]fields aren’t equal. Lombok is the exception: itsequalscompares arrays by content. In any of them, aListavoids the question. - Copies are shallow.
copy(),toBuilder()and calling a record’s constructor again all share each field’s object with the original. If a field is a mutable list, both copies hold the same list. Keep the fields immutable. - None of them is a good JPA entity. Records can’t be entities at all: they’re final, immutable and have no no-argument constructor. A Kotlin data class or a Lombok
@Dataclass can be, but itsequals,hashCodeandtoStringcover every field, which can trigger lazy loading, recurse between two entities, and change the hash code when an entity is saved. Use a regular class with@Getterand@Setter, not@Data, and base equality on the id. Records and data classes do make good query results: Spring Data JPA accepts them as DTO projections. - JSON needs a little setup for two of them. Jackson has read and written records on its own since version 2.12. Kotlin data classes need
jackson-module-kotlin, which Spring Initializr adds and Spring Boot registers. A Lombok class built only through its builder needs@Jacksonized, so Jackson knows to use the builder. On Spring Boot 4, which uses Jackson 3, you also need Lombok 1.18.44 or later, withlombok.jacksonized.jacksonVersion += 3inlombok.config: by default Lombok still generates Jackson 2 annotations, so the class either fails to compile or Jackson 3 ignores them.
🔁 Using Them Across Kotlin and Java Link to heading
- A Java record, used from Kotlin, reads like a Kotlin class:
customer.nameworks, without the parentheses. - A Kotlin data class, used from Java, has getters (
getName()) and a constructor that takes every argument in order. Java gets no named arguments, no defaults (unless you add@JvmOverloads), and acopythat needs every value. Mark the class@JvmRecordand Kotlin compiles it as a real Java record, so Java seesname()and pattern matching works. It needs a JVM 16 target, the class can’t extend another class or have mutable properties, and adding it to an existing class breaks binary compatibility, because the accessor names change. - A Lombok class, used from Kotlin, works when Lombok and the Kotlin code are in different modules: Kotlin just sees compiled methods. In a module that mixes Java and Kotlin, Kotlin compiles before Lombok runs, so it can’t see the generated methods. Kotlin’s Lombok compiler plugin fixes that for the common annotations, including
@Getter,@Setter,@Builder,@Data,@Withand@Value.
🧭 How to Choose Link to heading
| Your situation | Use |
|---|---|
| Kotlin code | A data class |
| Java, an immutable type with a few fields | A record |
| Java, many optional fields, or frequent copies with changes | A record with Lombok’s @Builder, or Lombok @Value and @Builder |
| Java, a mutable class such as a JPA entity | A regular class, with Lombok @Getter and @Setter if you like, not @Data |
| A type used from both Kotlin and Java | A Java record, or a Kotlin data class with @JvmRecord |
| Java older than 16 | Lombok @Value |
✅ Conclusion Link to heading
Records took away most of what Lombok used to be for, and Kotlin never needed it. The question that’s left is narrow: does a Java type need a builder or easy copies? If yes, Lombok on a record is a good answer. If not, the language already has what you need.
Sources
- Kotlin docs: Data classes
- Kotlin docs: JVM records in Kotlin
- Kotlin docs: Lombok compiler plugin
- JEP 395: Records
- JEP 468: Derived Record Creation
- Lombok: @Value
- Lombok: @Builder
- Lombok: @With
- Lombok: @EqualsAndHashCode
- Lombok: @Jacksonized
- Lombok changelog
- Jackson 2.12 release notes (record support)
- jackson-module-kotlin
- Spring Data JPA reference: Projections
- Spring Boot system requirements
Related Posts