With Kotlin, specifying a data type is optional.
When using Java, we always had to specify a data type. For example, we couldn't write
val name = "Daniel" but instead were forced to write
String name = "Daniel". We always had to define the type (
String, in this example).
However, Kotlin does not require a data type. Here's an example. This is valid Kotlin:
val firstName: String = "Daniel"
Notice how we have the
String data type. We can just as easily omit
val firstName = "Daniel"
Kotlin is smart enough to know that
firstName is a
String. We can just as easily provide an
val age = 25
When you need to specify a data type, sometimes it's enough that the class specifies it's type. Here is an example of a
ViewModel from Android Architecture Components:
val firstName = MutableLiveData<String>()
Kotlin knows that
firstName is now of type