Your First Program in Kotlin

The first step in Kotlin's adventure is writing a minimal program in this language. Yes, it’s the famous “Hello World!” program. This is…

Your First Program in Kotlin

The first step in Kotlin's adventure is writing a minimal program in this language. Yes, it’s the famous “Hello World!” program. This is what it looks like in Kotlin.

fun main(){ 
  println("Hello World!") 
}

This is minimal and easy to understand compared to other languages like Java, there is no need of the class. There are many more versions of this.

fun main(args: Array<String>) { 
    println("Hello World") 
}
fun main(vararg args: String) { 
    println("Hello World") 
}
class Test { 
    companion object { 
        @JvmStatic 
        fun main(args: Array<String>) { 
            println("Hello World") 
        } 
    } 
}
suspend fun main() { 
  println("Hello World!") 
}

But the first one is the simplest. All other forms also result in the same output. Let’s focus on that.

fun main(){ 
  println("Hello World!") 
}

Live Templates

IDEs based on IntelliJ idea (Android Studio and IntelliJ Idea) have built live templates for the main function in a valid context. if you start typing main or maina in a Kotlin file, you will be shown a suggestion that offers the whole main function.

Image showing intellij ide showing suggestion for main and maina live templates for main function
Intellij Idea

The difference between main and maina is main will give you the function main without any parameters and maina will give you the function main with a parameter of name args of type array of string (Second form of main function mentioned above).

What is under the hood on JVM

The most mature and important target for Kotlin is JVM. On JVM, every element needs to be defined inside a class. You might be wondering how it is possible that our main function can be started there even if it is not in a class. Let’s figure it out. On the way, we will also find out what our code looks like if that is written in Java. Java developers find it very useful for learning how Kotlin works. This is the simple program that we have already discussed.

fun main(args: Array<String>) { 
    println("Hello World") 
}

Now, select the tabs, Tools -> Kotlin -> Show Kotlin Bytecode

After that, You will get a window like this:-

This is the Java Bytecode that is generated from that source. As you can see, there is a decompile button on the top left in the right window. When you click on that, Java Bytecode will be decompiled into Java code.

This is the code that is generated:-

public final class MainKt { 
   public static final void main(@NotNull String[] args) { 
      Intrinsics.checkNotNullParameter(args, "args"); 
      String var1 = "Hello World!"; 
      System.out.println(var1); 
   } 
}

As you can see, Kotlin wraps that function inside a class named MainKt which comes from the file name Main and Kotlin appends Kt. You can customize the Jvm class name using the annotation @file: JvmName(“NewName”). Then the class name will be NewName.

At the moment, there is no tool like this for other targets, where we can see the generated code by Kotlin. I hope the Koltin Foundation will add it soon.

Packages and Imports

Whenever we are working on a project, most of the time our program spans multiple files. We use packages to structure them and use imports wherever we need that part of the program.

package com.codeancy.domain 
 
import com.codeancy.domain.model.User 
// or  
import com.codeancy.domain.model.* 
 
fun useUser() { 
    val user = User("Marcin") 
}

If we don’t specify a package, then the file is in the default package. Normally, We specify our root package as a reversed domain name and other packages as the directory name. Wherever we need any objects or functions from any other directory we use import statements to import it into that file.

That’s it for this article and if you are interested in personal training for Kotlin, KMP, or Android development? Contact me at virat@codeancy.com to learn more!