Kotlin Hello World Program - Getting Started

kotlin-hello-world-program


Let's start by creating a new project in Intelli J to create our very first app in Kotlin. Refer below steps -

1. Create New Project.

2. Select Kotlin and Kotlin (JVM). Then Click Next.

3. Name your project and select the path of JDK for Project SDK.

4. You need to give path of your JDK to run your Kotlin Program. 

5. Inside your src directory create a new file - Kotlin File/Class. Name it whatever you want. For example - HelloWorld.kt. 

6. New file will be created with an extension of .kt

7. Type below lines of code - 

fun main(args: Array<string>) {
    println("Welcome To CheezyCode!!")
}

This is a simple Hello World Program. In Kotlin, you do not need to define any classes to create a program. Just a simple main function that prints "Welcome To CheezyCode" onto console window.


kotlin-intellij-new-project-cheezycode


Let's dissect above Kotlin program - 


1. fun keyword is used to create functions in Kotlin. Here function name is main which accepts arguments of type String Array. This is similar to public static void main(String args[]).

2. In Kotlin, type of the arguments or parameter is written after its name i.e. in above code snippet, args : Array<String>. This depicts that args is of type String Array.

3. Above main function does not return anything. It's return type is void. To define your return type, you have to write it as -

fun greet( message : String) : String // return type of function
{
     return "Hello "+ message
}

4. Array is declared with a class name Array. Square bracket notation is not used ( [ ] ).

5. println is similar to System.out.println() in Java. It is just a wrapper around those method calls.

6. No semicolon is needed to end the line.

7. When you compile this program, class file is generated HelloWorldKt.class i.e. the bytecode that runs on JVM. Refer below image -


kotlin-helloworld-summary


These are the key features of this program. If you have background of Java or C#, you can understand this program easily. Stay tuned for more articles. Till then Happy Reading.





Comments

  1. Sir I like you teaching I want to talk with you and always connect with you

    Can you give me your contact number

    ReplyDelete

Post a Comment

Hey there, liked our post. Let us know.

Please don't put promotional links. It doesn't look nice :)

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example