Over a million developers have joined DZone.

Learning Kotlin: Data Classes

DZone's Guide to

Learning Kotlin: Data Classes

Want to avoid writing long lines of code in Java? Kotlin makes using data classes extremely easy.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Wow! Data classes are awesome! I really like how classes are handled in Kotlin. However, data classes are the pinnacle of that work. The goal of this tutorial is to convert the following Java class to Kotlin:

package i_introduction._6_Data_Classes;

import util.JavaCode;

public class JavaCode6 extends JavaCode {

    public static class Person {
        private final String name;
        private final int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;

        public String getName() {
            return name;

        public int getAge() {
            return age;

And, what does that look like in Kotlin?

data class Person(val name: String, val age: Int)

Yup, a single line. The data annotation adds a number of important functions (like ToString and copy). We then declare the class with a constructor that takes two parameters and both become properties.

Another important thing I learned with this information is that Kotlin has both a val and var keyword for variables. We have seen var already, and val is for read-only variables.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

java ,kotlin ,classes ,data ,code ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}