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 Person(val firstName: String = "", val lastName: String = "") {
fun foo(name: String = "") { }
}
With a default value defined for this method parameter, we can call the method from Kotlin without passing any arguments:
val person = Person()
person.foo()
But from Java, we must pass the argument value when invoking the function:
Person person2 = new Person();
person2.foo(); // wont compile
This difference highlights how convenient Kotlin features, such as default parameter values, may not be available from Java by default. In the next chapter, we'll take a look at how we can improve this behavior.