Create a Kotlin Project

Follow the below steps to create your first kotlin Project in IntelliJ IDE.

    • Double click the IntelliJ shortcut present in the Desktop or choose it from the start menu
    • Select Create New Project Option from the welcome screen

welcome-screen-intellij

    • Choose Kotlin as your project and Kotlin JVM module

choose-kotlin-kotlin-jvm-project

    • Choose the installed JDK at the place of Project SDK

kotlin-runtime-jvm

    • Expand the Project and right-click on the src folder and select New->Kotlin File

new-kotlin-file

    • Provide the name for the Kotlin File, this is not a class name we are just providing a file name

first-kotlin-file

    • Place below code in the above-created file

first-script-kotlin-function

    • Run the Script by Going to Run Tab and select Run <File Name>

run-first-kotlin-script

    • You might see the output like below

hello-mars-kotlin

Safe Call Operator [?.]

First Program in Kotlin

We have executed a program, let's discuss the components in the programs, I have highlighted them below.first-script-kotlin-function-highlight

    1. 1 & 8 Comments :

      Comments are nothing but informational lines for the user.

      The Compiler doesn't count the comments as the member for the compilation, so whatever is present in a comment, the compiler will ignore them.

      There two types of comments :

      • Multi-line comments : Whatever is present inside /* and */ will be considered as a comments
      • Single line comments : whatever present after // will be considered as a comment
    2. 2 fun ;

      fun is keyword used to denote that, this block is a function

    3. 3 main :

      this is nothing but a function name, it could contain any name, the main function is the place where the execution starts.

    4. 4 args :

      this is parameter name, a function can accept n-number of parameters. Parameter follows all the variables properties

    5. 5 Array<> :

      this is nothing but the parameter type, unlike other languages, In kotlin variable comes first and then the variable type. Types could be anything, here we are accepting Array with generic

    6. 6 <String> :

      this is nothing but the generic, here we are using the string type as generic. we will discuss more about generic on the generic tutorial.

    7. 7 braces :

      braces are used to contain the elements inside the block, if we write outside anything then it belongs to other function or class

    8. 9 println() :

      println() is nothing but a function which will direct the given value into the console of the ide/terminal

    9. 10 input value for println() :

      this value could be <

      Subscribe to See Videos

      Subscribe to my Youtube channel for new videos : Subscribe Now