June 2017
Intermediate to advanced
400 pages
8h 44m
English
To declare a variable or property, you must decide whether to make it mutable or not. Preceding a variable declaration with a val will make it only assignable once, whereas var is mutable and can be reassigned a value multiple times. The name of the variable then follows with a colon separating it from the type. Then, you can assign a value if you have it on hand. In the following code, we assign a variable for an Int and a String and print them in an interpolated string:
fun main(args: Array<String>) { val myInt: Int = 5 val myString: String = "Alpha" println("myInt=$myInt and myString=$myString") }
The output is as follows:
myInt=5 and myString=Alpha
Kotlin's compiler is pretty smart and does not always ...
Read now
Unlock full access