In Kotlin, the HelloWorld.kt
program can be written as a simple function like the following snippet.
fun main(args: Array<String>) {
println("Hello, World!")
}
From this little code snippet we can learn the following features of the Kotlin programming language:
- Kotlin program saved in a file with
.kt
extension. - The
fun
keyword to declare a function to show you that programming can be fun again 😉 - To declare variables we start with the variable name followed by its type separated by a colon.
- We don’t have to end a statement with a semicolons.
- To print we can use the
println
function, which is a wrapper to JavaSystem.out.println
. - The
main
function is the execution entry point of our HelloWorld program. - We can create functions at the top level file without creating a class.
To compile the HelloWorld.kt
program we run the following command:
kotlinc HelloWorld.kt
The compiler creates a class file called HelloWorldKt.class
. To run it type the following command, assumed that you’ve set up the $KOTLIN_LIB
environment variable. In my case the variable is set to /usr/local/Cellar/kotlin/1.2.40/libexec/lib
.
java -cp $KOTLIN_LIB/kotlin-stdlib.jar:. HelloWorldKt
Latest posts by Wayan (see all)
- How do I add an object to the beginning of Stream? - February 7, 2025
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
I really appreciate the enlightening article you wrote about kotlin. It was incredibly helpful and wonderful.