Considering a move to Kotlin? Coming from a Java background? In this short series of blog posts, I’ll take a look at familiar, straightforward Java concepts and demonstrate how you can approach them in Kotlin. While many of these points have already been discussed in earlier posts by colleagues, my focus is simple: how you used to do it in Java, and how you do it in Kotlin.

Case 2: You need a simple object.

Just data. No behavior. No clever tricks.

And yet, Java somehow turns this into a small project.

The problem

We want a simple Person object with:

  • a name

  • an age

  • sensible defaults

  • equality that actually works

This should be boring. In Java, it rarely is.

The Java way

Java gives us records, which are a huge improvement over traditional Java data classes. Still, even with records, extra features inevitably mean extra code.

public record JavaPerson(String name, Integer age) {

  public JavaPerson() {
    this("John Doe", 100);
  }

  public JavaPerson copy(String name, Integer age) {
    return new JavaPerson(
        name != null ? name : this.name,
        age != null ? age : this.age
    );
  }
}

Usage:

JavaPerson p1 = new JavaPerson("Jan", 42);
JavaPerson p2 = p1.copy("Piet", null);

This is clean Java. Modern Java, even.

But:

  • Defaults require extra constructors

  • Copying requires manual null checks

  • Intent is hidden inside boilerplate

The Kotlin way

Kotlin introduces data classes.

data class KotlinPerson(
    val name: String = "John Doe",
    val age: Int = 100
)

That’s the entire class.

Usage:

val p1 = KotlinPerson("Jan", 42)
val p2 = p1.copy(name = "Piet")

Out of the box you get:

  • equals() / hashCode()

  • toString()

  • copy()

  • destructuring

  • default values

No extra code. No tricks.

Why this matters

Data classes make intent obvious: “This object is just data.”

Java can do this. Kotlin makes it effortless.

Less code means fewer bugs. And fewer bugs means fewer meetings. Everyone wins.

Takeaway

Java records are great. Kotlin data classes are… boringly perfect.

And boring is exactly what you want for data objects.

shadow-left