Kotlin Hello World - First Kotlin Program
Let's write the first Kotlin program - Hello World in Kotlin. We will look into all the elements required to write this simple program in Kotlin. But before jumping directly into our program, we will first have a look at - Functions.
What are functions?
- A function is just a set of instructions that does something i.e. a block of code that performs a task.
- For e.g. you go to a pizza shop - you order your pizza with all the toppings you want - Pizza Shop makes it for you and you get your pizza. Here pizza shop behaves like a function that does something based on the inputs (toppings and the type of pizza) and provides you the output i.e. your special pizza.
- Few more examples from real life -
NOTE: Method and Function are synonymous. They can be used interchangeably. (Some people will argue but it's ok - we are here to learn)
- Similarly, in programming, we create a method that does something. We write instructions in it for the computer to execute. Now, whenever we need this functionality we call this function/method.
How to write a function? Syntax of Function in Kotlin
fun nameOfTheFunction(input) { //code or instructions return output }
- This is how we write a function in Kotlin. Now whenever we need this functionality somewhere in our program - we will use this function (i.e. call this function that - Hey Function !!! We need you - here are your inputs, just do the magic for us and give us the output)
Hello World Program In Kotlin
- There is a special method/function that runs when we start executing our program. Our program starts execution by calling this function. This special method is known as the Main Method
- If you have multiple Kotlin files, JVM looks for a file where the Main method is defined and executes that method. So we need to write the main method - this is the entry point of our program.
fun main() { println("Welcome To CheezyCode") //prints Welcome To CheezyCode in console }
- This is how you define the main method. The only instruction defined in this main method is to print Welcome To CheezyCode. You can write multiple statements inside this main method.
fun main() { println("Welcome To CheezyCode") print(2 + 3) //prints 5 print(false) //prints false }
Note: println prints the output and move the cursor to the next line whereas print just prints the output.
Compilation
- Now, what happens behind the scene when you run this program - Kotlin Compiler runs to check the errors in your code and convert that code into something that JVM can understand. This compiled code is what is known as Bytecode.
- This bytecode is then passed to JVM to run the code, JVM starts executing instructions defined in the main method. Refer to the image below that shows you the process.
That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.
Happy Learning !! Cheers From CheezyCode.
Comments
Post a Comment
Hey there, liked our post. Let us know.
Please don't put promotional links. It doesn't look nice :)