A Hello World application in Kotlin

A simple hello world programme

This tutorial assumes you have some experience with Java, are already familiar with the IntelliJ IDEA IDE, and have a recent version with support for Kotlin installed. It also assumes you have already configured IntelliJ so use the Java SDK. It will take you through the creation of a basic Kotlin application.

  1. Launch IntelliJ IDEA, closing any projects that open automatically
  2. Create a new project, and configure it as shown in the image below. This will create a Kotlin project that will execute its code using the Java Virtual Machine New Kotlin Project dialogue box
  3. Ensure you name the project 'HelloKotlin' and store the project somewhere you can find it later, click finish to create the project.
  4. We will be provided with an empty Kotlin project, so the next step is to add a file to write some code in. Expand the folder structure as shown below adding a new file to an IntelliJ iDEA project
  5. Right click on the src folder in the project navigator and choose New -> Kotlin file/class
  6. Name the file 'Main' Net Kotlin file dialogue box
  7. If you have worked with Java, you'll likely be thinking that we need to create a Main class and a static 'main' method. Kotlin allows us to add functions outside of any class (remember a method is just the name for a function that belongs to a class), so we don't need the Main class. We do, however, need a main function. Add the following code directly to the file:
    fun main(args: Array<String>){
    
    }

    You'll notice the code is a little different to Java, firstly the fun keyword indicates that we are creating a function. Next is the name of the method, and in the brackets we see the parameters name and type. Note that the order is inverted from Java - the name of the parameter is given first, followed by its type (in this case an Array of String). Note also, that this method does not return a value

  8. From the IntelliJ IDEA menu, click Run then Run... If prompted, choose 'Edit Configurations'
  9. In the box that appears, choose the + button in the top left hand corner and choose Kotlin from the drop-down list
  10. In the dialogue box that appears, type MainKt in the Main Class box, then click Run.
  11. Verify that the programme launches, and terminates successfully.
  12. Modify the programme to output as message to the console as follows:
    fun main(args: Array<String>){
        println("hello world")
    }
    Note that a semicolon is not required at the end of the line
  13. Run the code again, and ensure that you see the output in the console

Hello world with a function

To enhance the programme, and to get familiar with Kotlin functions, we might create a function which will take a name, and return the greeting "Hello", followed by the name, appropriately spaced

  1. Add the following method stub, which will take a String as a parameter, and also return a String. Be aware that the return type appears after the method name its parameters and a colon
    fun greet(name: String) : String {
        
    }
  2. At this stage you'll see a warning that the method does not return anything, add the following line of code, inside the body of the method, which will return a string with the word hello, followed by the name
    fun greet(name: String) : String {
        return "hello $name"
    }

    Note that Kotlin allows us to use the String representation of an object inside a String literal by using the $ symbol to escape the variable, this is know as a String template.

  3. Next, modify the code in the main function to call the greet function passing your name as a parameter (replace the name string with your own in the example here)
    fun main(args: Array<String>){    
        var greeting = greet("Andrew")
        println(greeting)
    }
  4. Run the code to check that it works as expected, then modify the name String and test it again

A final improvement

After the greeting variable is initialised, it is never changed, therefore rather than declaring it as a variable (using the var keyword), we would be better to declare it as an immutable variable (i.e. a constant), the keyword for which is val

Modify the code to do make greeting immutable