Before introducing the program syntax, I think you need an environment that can run Kotlin, it is recommended to open it in a browser: https://developer.android.com/training/kotlinplayground
You should see the following screen:
You can key in your code in the white block and clock the green arrow to execute the program. The program output result will be displayed below.
The basic program: Print “Hello World”
First, we need to mention a few Kotlin special words (or you call them keywords).
fun
stands for function. different functions are usually designed for different functions (features), which can also make the code more concise.
main()
is a function name but it is a special one, it is the entry point of program, which represents the first function that run after the program starts to execute.
println()
is used to print text.
Then we look at the most basic program:
fun main() { println("Hello World!") }
Output:
Hello World!
When writing a program, don’t forget to put double quotes around "Hello World"
. In this way, it can mean that this is a string and not any variable.
We can change the content of string if we want, and we can also add more println()
functions.
fun main() { println("Today is a nice day!") println("I want to go to play!") }
Output:
Today is a nice day!
I want to go to play!
References
- https://developer.android.com/training/kotlinplayground
- https://developer.android.com/codelabs/basic-android-kotlin-training-first-kotlin-program?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-one%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-first-kotlin-program#2