Basic Kotlin Program Example

Kotlin Tutorial – In this tutorial we shall learn about the typical structure of a Kotlin program with and example.

Basic Kotlin Program Example

Kotlin supports procedural programming, like C and C++, in addition to Object Oriented Programming(Classes and Methods). Using the following basic Kotlin program example, we shall make some useful observations.

“main” function is the entry point for a Kotlin program. Consider the following Kotlin program, HelloWorld.kt

1

2

3

4

5

fun main(args:Array){

println("Hello World !")

}

1

2

3

Hello World!

Let us together make out some observations from the above three lines of Kotlin code:

The keyword “fun” is used to define a function.

We have only one function, i.e., “main” in the Kotlin program, HelloWorld.kt. Since main is the entry point for a Kotlin program, the program is compiled successfully and run successfully.

The entry “main” function can accept Array of Strings as an input (arguments) to the function, which are the inputs to the Kotlin program.

The arguments’ type is mentioned after the variable name separated by a colon.

The function body is enclosed in curly brackets.

There is no semicolon at the end of println statement to denote the end of statement. Kotlin compiler can make out the end of a statement with a new line or semicolon. So, semicolon is an optional means of ending a statement in Kotlin.

Conclusion :

With the help of this Kotlin Tutorial – Basic Kotlin Program Example, we have learnt about the basic building blocks and structure of a Kotlin Program.