Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Your First Program

Hey there, I'm Anna and in this lesson we will be learning creating a new project in Kotlin. We will write our first Kotlin code and execute it. So without further delay, let's get started. We had this window from our previous lesson, so we'll create a new project. Clicking on it, here is the window for the new project. We will select Kotlin and we need Kotlin/JVM, there is also option for Kotlin/JS, Kotlin JavaScript. Which means that we can write our codes in Kotlin and convert it into JavaScript. We will be dealing with Kotlin/JVM, so select it and then click on Next. Let's give the project name as MyFirstKotlin. Set the project location and here we need to set the path to JDK. You need to have the JDK installed in your system, and then select the path to it. I will select the path to my JDK. For the library, the Kotlin Java run time is selected by default. So now, we are done with filling out the details for the new project. Let's click on Finish and here is our new Kotlin project setup. On starting a new project or opening the IntelliJ IDEA, we get the tip of the day. If you want it to show each time iou start the IntelliJ IDEA, check this box, and if you don't, uncheck it. But I like those tips on every startup, so I'll keep it as it is, and for now, I'll close it. Our Kotlin project is getting configured. So here, make sure you're in the project hierarchy of your Kotlin project. On clicking the project folder, MyFirstKotlin, we have a source directory here, in which we will be having all our Kotlin files. Let us create a new Kotlin file in our project and start writing the code. Right-click on the source folder > New, and we will have a new Kotlin file class. Let's give it the name, FirstKotlinDemo. The kind should be file, make sure it is file. We have other kind which we can create, that is the class interface, Enum class, and object, which we will deal with later. For now keep it as file, click on OK, and here we are with our first Kotlin file. Let's write the code for printing a simple Hello World. To start it, we need to create the main function in Kotlin, fun is the key word that is used for defining a function. Unlike any of the programming language, we do not need to end the statement with a semicolon. This shows how less ceremony we have in Kotlin. So basically, a syntax for writing a function in Kotlin is, writing the fun keyword, which stands for function, here is the function name, then we have the parameter, here we have args, and then we have a colon here. After which we define the data type, or the type of this parameter. Here we have an array of string type, and then we have the function body. This how concise the Kotlin code is. Now, let us run this block of code and see if it works well. Before proceeding forward, let me show you a few ways which I'll be using to run the Kotlin code, so please don't get confused if I use any of this. The first one will be clicking on this Kotlin icon, and then Run FirstKotlinDemoKt. Now this file is FirstKotlinDemoKt, this is the class file that the Kotlin compiler has created. We will talk about it after some time, but for now, to execute the code, we will click on Learn FirstKotlinDemoKt. Here is our program running and congratulations, you have executed your first Kotlin code. The second way in which I would run is clicking display button in a menu bar. Or you may also press Shift+F10 to execute or to run your Kotlin code. The third way I may use is clicking on Run and click on this, Run FirstKotlinDemoKt, which is the file name or you can directly click on this option, Run. So here are the few ways which I'll be using to run the Kotlin code. I had said that the Kotlin code is very concise as compared to Java. If it were Java, the code for executing this println, Hello World, would have been somewhat like this. First we had to declare a clause and name the clause, and then we would write the code for the function. Public static void main and then the data type, the parameters name, and then we had to write system.out.println and then Hello World. But in Kotlin, we don't need all this, we just need the fun keyword for the function, the function name, the parameter name, and the data type. Now, let us see where did this first KotlinDemoKt come from. The Kotlin compiler automatically converts the Kotlin file to the class file. We can see here Run, clicking on the run menu, and then Edit Configurations. We see the main class here is already created as first KotlinDemoKt, whereas our class name was FirstKotlinDemo.Kt. So the Kotlin compiler automatically creates this class out of our Kotlin file. So this is the beauty of the Kotlin, very less ceremony, and so many things done. You can also see this class file in the out directory, and here we have the class file FirstKotlinDemoKt.class. Our Kotlin code is converted and compiled to this and here we see this unit keyword. This unit specifies the written type of our function. Let's close this class file for now and here, let me give you a brief introduction to the unit keyword. Here in the function definition, after you have your parameters defined, give a colon and this unit. Unit is the keyword for the written type. Since we have nothing written in here from our main function, the unit is the written type for our main function. And this unit, to be returned as a return type, is optional. Like we have void in case of Java, we have unit in case of Kotlin. If we had an integer returning from this function, let's say, written 5, or written any other integer, this certain type would have changed to int. If we had a string returning, it would have in string. And this way, the written type for the function is written. Let's remove it, and let's print something else. Let's say 5 + 5, and now let's execute our code. We have been, as the output here, let's print something else. Let's say Envato Tuts+. Now, run the code, I'll use Shift+F10 and here, we have Envato Tuts+ printed. And here, we can see the difference, println and print. If you have println, the next print statement will be printed in the same line. Like here, we have 10 and Envato Tuts+. But if we have println, the next print statement will execute the string, or the output, in the new line. Here is the difference between the print, and println. Println prints in the new line and print prints the next output in the same line. Play around with print and println. We shall meet in the next lesson. Until then, happy coding and have a good day.

Back to the top