String Array
Make an array of words.
1fun main() {
2 val greetingWords = arrayOf("hello", "world", "!")
3 println(greetingWords.contentToString())
4}
$ kotlinc Main.kt -include-runtime -d Main.jar
$ java -jar Main.jar
[hello, world, !]
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 greetingWords = arrayOf("hello", "world", "!")- arrayOf(...) makes an array of any kind of object; here Strings. (intArrayOf and booleanArrayOf are faster arrays made just for numbers and booleans.) Each String still needs its own double quotes: greetingWords[0] is "hello".
println(greetingWords.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 }.