Hello World!
Today we will be working on the Hello World with Kotlin.
I will be using IntelliJ IDEA through this course.
Let’s see the code first then we will talk about it.
fun main(args: Array<String>) { println("Hello, World!") }
- No semicolon after the println.
- We used println directly instead of System.out.println().
- No need to import any classes.
The Kotlin Standard Library
Kotlin is striving to reduce the amount of code that we have to type, and so it imports a whole bunch of stuff by default into every Kotlin, and most of what it imports belongs to what’s called the Kotlin standard library.
Now if we checked the online documentation for Kotlin standard library it will show you all the packages that it creates for us.
and if we entered the kotlin section, we will find the Array class (which we used in the hello world) also belongs to the Kotlin standard library, and that’s why we didn’t have any import for the Array class.
Now if we clicked (Ctrl +B) in IntelliJ IDEA while selecting the Array from our code (or right-click, Go to -> Declaration), IntelliJ IDEA takes us to the declaration of Array, And we will see that it belongs to the Kotlin Package.
Now if we did the same for println, we see that it belongs to kotlin.io.
Now let’s go back to the documentation again, we will see the list of the default imports
Now we will see that kotlin.* is there and that is where the Array class belongs to, and the Kotlin.io.* which is where println came from.
Just a reminder, that if you’re going to distribute your Kotlin application that you also have to distribute the Kotlin runtime, otherwise your application won’t run.
Now that’s it for this lesson, in the next lesson I will be talking about the way kotlin deals with equality and how it differs from Java.