How to write the first program in Kotlin

Welcome to the second part of the Kotlin tutorial, In this chapter, we will write our first “Hello World!” Kotlin program in IntelliJ Idea. So without any further discussion let’s write the first Kotlin program step by step.

How to write the first program in Kotlin

(step by step)

  1. First, download and install the IntelliJ idea, if you already have Intellij Idea then skip this step.
  2. Create a new project.
  3. After successfully create the project, go to Project>Right click on src>select new> create Kotlin class/file.
  4. After creating a class you can remove the class block as of now.
  5. Simply write Kotlin program.
fun main(){
print("Hello World")
}

6. Run the program. After you run the above code, it will generate the following output in the console.

Hello World

Note: If you get any type of error please double check your program

Let’s discuss the program.

fun main(){
print("Hello World")
}
  • fun – The program starts with fun, fun is the function which is the keyword used to declare a new function, the function is the block of instruction surrounded by curly braces.
  • main – main is the name of the function, we can give our function a different name but the main is a special purpose. It is the entry point of every programming language, if you change the function name like- main to mainXyz then the compiler treats this function as a normal function because the compiler starts executing the program from the main function.
  • println – println stands for print line and this is what it does, it prints the given message inside the quotation mark and text will print to the console, println is also a function but we can not declare it as a function, we never wrote fun println() somewhere. we can only use the print or println function for prints the given message purpose and we just called any executed because this function belongs to the standard library

Note- In Kotlin, you can write the function outside the class or inside the class but in java, you can’t run programs without class.

fun main(){
print("Hello World")
}

In the program, as you can see here we never define the class

finally, we completed the second part of the Kotlin tutorial. In the next part of the Kotlin tutorial, we will discuss about variables in Kotlin.

I hope you liked the Kotlin tutorial post. If you have any questions regarding this post. Feel free to comment and share the post with other developers.

Happy Learning!!!

Read Next
    Click Here ⇝ Basic syntax of Kotlin

    Leave a Reply

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