February 2020
Intermediate to advanced
412 pages
9h 36m
English
To declare a variable or property, you must decide whether to make it mutable. 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 (the ch12_01.kt example), 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 ...
Read now
Unlock full access