Kotlin provides data classes as a powerful and simple way of creating more complex data models by combining its basic data types.
We’ll introduce data classes now before going much further, partly because they are quite simple and elegant, but also because they will make it more interesting to talk about collections and map-reduce-filter.
Kotlin inherits Java’s object model—a powerful tool for modeling and working with real-world data. However, working with data using Java classes requires a great deal of unnecessary ceremony.
Imagine that we want to create a
Person class to store a person’s name (as as
string) and age (as an integer).
To conform to modern Java coding conventions we’ll avoid public fields.
We’d also like some default values allowing us to contruct person objects even
if we don’t know the name or age yet.
And maybe even a nice
toString method to help with our debugging.
Is that so much to ask, Java?
(Don’t forget to scroll down!)
32 lines of non-commenting code later, we have a basic class that we can use to model a simple piece of data with two fields. I don’t know about you, but I’m ready for lunch! I hope our data model never changes—and, if it does, that I’m paid by line count.
Python is known for its data analysis capabilities. So, happily, it does a bit better than Java
class Person: def __init__(self, name = "", age = 0): self.name = name self.age = age def __str__(self): return self.name + " " + self.age
And, if you utilize the data classes annotation introduced in Python 3.7 (mid-2018), you can do even better:
@dataclass class Person: name: str = "" age: int = 0 def __str__(self): return self.name + " " + self.age
Also notice the type hints shown in the example above, introduced in Python 3.5. Neat! Go Python!
Here how to accomplish the 32 lines of Java ceremony above or 6 lines of fairly mindless Python in Kotlin:
And… we’re done. Four lines. Could be one, but I’m trying to not make Java blush too hard.
To simplify working with data, Kotlin includes data classes. To define a basic data class, all you need to do is provide a list of property names, types, and optional initial values that you need to model your data.
And, just like variables, we need to mark our data class properties either
var) or immutable (
Immutable data class properties can only be set in the constructor.
We can use both positional and named parameters when calling Python class constructors, in a nice bit of syntax that echoes Python:
Defining full Kotlin classes can be as simple as defining a data class:
So what do we get by adding the
data keyword to our class definition?
Lots of nice defaults that are useful for working with data!
Note also that in Kotlin we can test object equality using
==, rather than
The copy constructor also allows us to override fields from the copied object, even immutable ones:
data keyword brings features that make it useful for supporting basic work
However, data classes have some limitations compared with non-data (normal?)
Kotlin classes, which you can review
Since we haven’t talked about non-data classes yet limitations like not
sealed probably don’t make much sense yet.
But it’s worth pointing out a few limitations that Kotlin data classes do not have. In particular, data classes are not just limited to storing data—like the older concept of a record. Just like the Java or Python objects you may be familiar with, Kotlin data classes can define methods:
We’ll return to the limitations of data classes when we introduce full classes in a few lessons.