Mixed Type Array
One array holding text, a number and a boolean.
1fun main() {
2 val mixedItems: Array<Any> = arrayOf("hello", 2, false)
3 println(mixedItems.contentToString())
4}
$ kotlinc Main.kt -include-runtime -d Main.jar
$ java -jar Main.jar
[hello, 2, false]
Every line, explained
fun main() {- Every Kotlin program starts at the main function. When you run the program, Kotlin looks for fun main() and runs everything between its { and } from top to bottom.
val mixedItems: Array<Any> = arrayOf("hello", 2, false)- Any is the most general type in Kotlin: every other type (String, Int, Boolean, ...) is also an Any. So an Array<Any> can mix text, numbers and booleans. Here we write the type out with : Array<Any>, because otherwise Kotlin would only allow one type. Usually you keep an array to one type, but this shows that everything in Kotlin is an Any.
println(mixedItems.contentToString())- Careful: printing an array directly, println(arr), does NOT show its contents in Kotlin! It prints a strange code like [I@1b6d3586 (the exact letters depend on the array's type). arr.contentToString() turns the array into readable text like [1, 2, 3] first, and that is what we print.
}- This closing curly brace } ends the main function. In Kotlin every opening brace { must have a matching closing brace }.