October 2018
Intermediate to advanced
370 pages
9h 15m
English
Kotlin allows us to assign default values to constructor parameters. The compiler automatically assigns default values if the object is created without passing the relevant values to the parameters:
class Person(val name: String, var age: Int = 0, var height : Double = 0.0 )fun main(args: Array<String>) { val jon = Person("Jon") println("name ${jon.name}, age ${jon.age}, height ${jon.height}") val abid = Person("Abid", 40) println("name ${abid.name}, age ${abid.age}, height ${abid.height}") val igor = Person("Igor", 35, 6.0) println("name ${igor.name}, age ${igor.age}, height ${igor.height}")}
Take a look at the following output. The default parameter is a really powerful option provided by Kotlin that ...
Read now
Unlock full access