In this tutorial, you will learn how to execute your first Kotlin code and understand what are the minimal things you need to kick start programming in Kotlin. Let’s start by looking at a sample program.

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

Let’s go through the code line by line and learn the structure of the code.

fun main(args: Array<String>) {
    println("Hello World!")
}
// fun: Keyword to represent function
// main: Name of the function
// Array<String>: Arguments of the function
  1. fun main(args: Array<String>)

This main function is the entry point for the compiler to start executing the code hence, it is a mandatory function. 

fun is the keyword that indicates the start of a function followed by the name of the function, in this case, it is main.

Then inside the parenthesis, we have a list of arguments. This list can also be empty or have any type of data or objects.

  1. Open and close curly bracket

The open curly bracket { denotes the start of a block of code, whereas the close curly bracket denotes the end. Note that, if there is only one statement between the open and close brackets, then these brackets are not necessary, but if there is more than one statement inside a function, then we must write them inside the curly brackets. 

  1. println(“Hello World!”)

println is a function provided by Kotlin to enable us to print the phrases written inside of the bracket.  In this case, this statement prints the text Hello World! to our console.

Type in the code in your IDE and see the result for yourself. Well done! you just finished your first program in Kotlin. Click on the next button to continue reading.

Category
Tags

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *