October 2019
Intermediate to advanced
434 pages
11h 54m
English
Two of the most interesting and convenient features of Kotlin are default parameter values and named arguments. However, these features are not always available to us by default in Java.
To illustrate this, we'll start by updating the Person example to use default values for each parameter:
open class Person(val firstName: String = "", val lastName: String = "")
Now, we can instantiate Person from Kotlin without passing any parameters, like this:
val person = Person()
In this case, we can do the same thing from Java because the compiler will generate an empty default constructor that provides the default values:
Person person2 = new Person();
However, the same is not true if we add a method to the Person class:
open class ...Read now
Unlock full access