Friday 9 July 2021

Android Java to Kotlin migration by example part 4: Data classes

 Today we'll see a Kotlin feature, which is not available in Java: data classes.

Let's consider a following Java class:

Listing 1 (Java)

class Human{
String name;
int age;

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

@Override
public String toString() {
return "Human(" +
"name=" + name +
", age=" + age +
')';
}
}

We could "use" it this way:

Listing 2 (Java)

public class Main{
public static void main(String []args){
Human john = new Human("John Watson", 30);
john.age = 31;
System.out.println(john);
}
}

with the following result:

Listing 3 (result)

Human(name=John Watson, age=31)

Now, this class is kind of class that in Kotlin would be known as data class. Which means it doesn't provide any method, just fields (and one constructor, allowing to initialize them).

And the code analogous to Listing 1 would be:

Listing 4 (Kotlin)

data class Human(var name: String, var age: Int)

... that's right! That's all, one line. The data modifier results in generating a class having all the fields and the default parametered constructor, as well as some other methods, including toString() implementation, printing . The main method in Kotlin would be as follows:

Listing 5 (Kotlin)

fun main(args: Array<String>) {
var john = Human("John Watson", 30)
john.age = 31
print(john)
}

.. And that's all 24 lines of Java code become 6 of Kotlin!

What more can we do with data classes? Well, we can define the default (parameterless) constructor, like this code in Java:

Listing 6 (Java)

package com.company;

class Human{
String name;
int age;

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

public Human(){
this.name = "Sherlock Holmes";
this.age = 29;
}

@Override
public String toString() {
return "Human(" +
"name=" + name +
", age=" + age +
')';
}
}

public class Main{
public static void main(String []args){
Human john = new Human("John Watson", 30);
System.out.println(john);
Human sherlock = new Human();
System.out.println(sherlock);
}
}

...making the "default" human Sherlock Holmes :) 

Listing 7 (result)

Human(name=John Watson, age=30)
Human(name=Sherlock Holmes, age=29)

The only modification in our Kotlin code from Listning 4 would be adding these default values to our data class fields. So the whole program becomes:

Listing 8 (Kotlin)

package com.mypackage

data class Human(var name: String = "Sherlock Holmes", var age: Int = 29)

fun main(args: Array<String>) {
var john = Human("John Watson", 30)
println(john)
var sherlock = Human()
println(sherlock)
}

So over 20 lines of Java code (the Human class definition) became 1 in Kotlin!

Although data classes are quite specific (well, usually classes have some methods more sophisticated than toString() ), if such a simple class is needed - Kotlin code can become extremely short and powerful.

No comments:

Post a Comment

Python crash course part 10: inheritance and polymorphism

In the last part we've shown how to create and use a class in Python. Today we're going to talk about inheritance: wchich means cre...