Fundamentals of Kotlin

First Kotlin Program
Overview
Difficulty
Beginner
Duration
1h 43m
Students
12
Ratings
5/5
starstarstarstarstar
Description

This course covers the fundamentals of Kotlin, looking at the building blocks of the programming language and how they can be used to build apps in Android.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Transcript

Well, hello my friends. So, in the previous lessons, we completed the JDK installation. We completed Android Studio installation. We had a tour of the interface of Android Studio. So other words, we are set, ready and primed, okay, to develop android applications using this Kotlin Programing Language. So now, why don't we just get started? So, go to Android Studio and do a few necessary actions. If you remember, we already have a project called First Project, which we created while installing Android Studio, and we have it leftover from the previous lessons. So, our screen look like this. So, before we delve into Kotlin, I want to show you how to close and open project in Android Studio and then create a new project.

So, to close an existing project in Android Studio, my friends, we can use the File tab in the upper left corner of the screen, and after clicking on the 'File Tab' we can select the 'Close Project' option from here. In this case, our project in Android Studio will be closed. So as you see, we encounter a pop up here when you first start Android Studio. So if you notice, the First Project that we created earlier appears here. So in this area, the projects that we have created before appears so that we can open any project just by clicking on them. It's  convenient. So, if a project that we have created before is not actually displayed in this area, we can just select the 'Open existing project' option to open that project. All right so, after choosing that we'll need to access the file containing the project that we want to open. At this stage, you know where you save your projects.

So, my projects are saved in the Android Studio project folder by default. That's why I don't change that. So, if we select 'First Project' from here and press the 'OK' button, we can open up the project, and of course to create a new project, because that's what we're setting out to do, I just want to choose the create 'New Project' option here. And I select the empty activity option, and the 'Phone and Tablet' tab and press 'Next'. And from this page, we will configure the project that we will create, my friends. It's all right. "FirstKotlinProgram", in the name of the project, you can of course write whatever you want. We can change the package name or it can stay like this. If you want to, you can write your surname instead of the example here, it's all right. "techmania" here. And, then this will be where the file gets saved, and I'm not going to change that. 

All right, so our programing language will be Kotlin and the minimum SDK level, I would set to 23. All right so, after making these adjustments, I'll just press the 'Finish' button and wait for the project to be created. Cool. So, when you create the project for the first time, it may take a few minutes. So, we don't really get to take any action here before the project is completely created. All right, because if you start messing around, you might get some errors. All right, my friends. So let's check it out from the 'Build' tab. As you can see, our project has been successfully created, so now we can take a few wonderfully necessary actions. Before developing Android applications, I want to show you how this Kotlin language works.

Some of the basic coding in Kotlin, so we're not going to actually use this space here right now my friends, and we will not make any designs either, but I already pre-warned you so. There you go. So, why is this? Well, we're not going to develop an application right now, because I really do want to show you the basics of Kotlin, and some of the most frequently used commands as well. So, after we learn Kotlin really, really well, and you will learn it quickly, then we can develop, well, at a much swifter pace perhaps, really great Android applications by using all of the information that you have learned up to that point. So my friends, I have explained enough, that's enough; legal disclaimer.

Let's move on to learning Kotlin. So for that, we need to create a new Kotlin file and we'll do some of the necessary operations in this file. So, create a new file in Kotlin as follows. Select the package name in the Project Directory and right click, select 'New', 'Kotlin Class/File', and that's from the option window that opens, so as you can see, a window pops up, and from this window we choose File option not the Class. After selecting the 'File' option, we need to give this file a name. Now in this lesson, because it's the first time we will print an expression, namely "HelloWorld", to the console using Kotlin. So therefore, I will set the name of the file that I will create to be: "HelloWorld". Have you noticed I always capitalize the first letters of the file that we create here. I don't leave any spaces in between two words.

Now, since the word Hello World or words I suppose, are two different words. We capitalize the first letters of each. So, this is the general protocol. Now another spelling is "Hello_world", but your first word should usually be capital. All right, so now I'll just correct that again. So after typing, "HelloWorld"; press the 'Enter' key on the keyboard and as you can see a new Kotlin file gets created. Cool. So, now we will print HelloWorld to the console using this Kotlin file. And here I would like to point out that normally in order to perform an operation in the Java Programing Language. Well, there must be at least one class, at least one main function in the class, but you don't need a class in Kotlin. So, it's not mandatory here. Therefore, instead of creating a class here, we will first create a function. Of course, you might be a little more unfamiliar with these concepts like class and function, but don't worry about that. We'll get into those weeds later in our next lessons.

So, currently you only need to specify that there must be at least one main function in order to run a program in Kotlin. Because when you run the program, the Java virtual machine has to know where to run the code. So therefore we will create the main function here. So, how is the main function created? Well here I just write "fun", okay, to represent the Function, and press 'Enter' on the keyboard. So, if you pay close attention when you type in fun, Android Studio offers us some options. And from here you can select the 'fun' option and press 'Enter'. Cool. So, next we need to give a name to the function that we've created. Now, since this function is our main function, we will write "main" here.

Now, notice when we write main here, we can understand that this is an executable function with a green triangle.  So, now we can move on to creating the functions or after typing "fun main", open a parentheses here, and we'll need to specify the parameter, that is the argument of this function in parentheses, and we'll specify the argument as follows. We write "args", that is argument in the parentheses, and I put a : and write "Array" in here, and select the 'Array' option. Then, when we press the <> key on the keyboard, we create a tag, that is chevrons. Now in addition to this, we'll need to specify the type of this directory in the tag. So, I'll just write "String" here and finally, we need to specify the scope of this function. So, we use curly braces for that. By passing in the Enter key, I'll move to the next line, when I press the Alt F7 keys on the keyboard, "{}" appear just like magic. So, I press 'Enter' again.

So, in this way we have created our main function. So, let me just show you again and explain it. So, notice here that the function starts with the expression "fun". The name of this function is "main". Also, this main function takes a parameter, and this parameter is an "Array". The type of this Array is "String". And finally we specify the scope of the main function with "{}". So, if we're going to print something to the console, we have to write it between these two "{}", you see? So, to put that more precisely, when we run the program, the codes that we write in this main function will run. So, if you want to just go ahead and print HelloWorld to the console now.

So, how would you print an expression of the console in this Kotlin Programing Language example that I'm showing you? So, for this we'll just use the "System.out.print" function in Java. But in Kotlin we will only use the "print" function for this. So, after you write the print function, you can write an expression that you want to show on the console in parentheses. Of course, since we're going to print Hello World, that is a string expression, I write Hello World between " marks, my friend. Yes so, actually when we run the program, we will see the Hello World on the console.

Now, of course you might be thinking, well, why did we put  " marks like that? This is actually a syntax form of string expressions. So, in our lessons you will be seeing this a lot and we will see variable types such as strings and integers, but for now, that's enough to know. So, how can we print an expression to the console using this Kotlin Programing Language? Well, let's now run this program, and here's how we run the program in Kotlin. Select the file that we want to run from the Project Directory, right click with a mouse. Select the file named 'Run HelloWorld' from here. When you choose as a console screen, well, open at the bottom of Android Studio, and we can see the output of the program on this screen. And while the program is running I would like to underline the following once again, we are currently learning to print an expression on the screen using this Kotlin Programing Language.

So, what I want you to do is first of all, if you just watch the lesson and complete the steps that I've done, by the time you know a little bit more about Kotlin, you will certainly understand these operations. Well, what's the expression, "like the back of your hand", you'll know it like the back of your hand. I don't spend too much time looking at the back of my hand, but you get the point. So my friends, you can see now that the program works. We see "Hello World" on the console. Okay, so we printed Hello World on the screen using Kotlin. So, let's go over what we did.

So, since we've only learned a little bit and we're not developing any Android applications yet, we did create a new Kotlin file. We will continue our operations through this Kotlin file. And also, if you want to run a bit of code in Kotlin Programing Language, we need at least one function. The name of this function that we created is the "main" function. So, we created the main function as follows: First, we needed to specify that it is a function, so that's why we write "fun" here, and we write the name of the function, namely "main", and we needed to find an argument to it in parentheses, and we define it like this; "args" represents the argument.

After writing a :, we create an Array of the type "String", we specify the scope of this function using "{}", so we specify the area, okay, that it will cover. And finally, we can write the codes that we want inside these two {}, and we printed Hello World to the console. And then how do we actually print? Well, we print this with the "print" function, so we put it in " marks inside the parentheses. Hello World is a string, and in order to run the program, we choose our file from the project directory, right click with the mouse and choose the 'Run HelloWorld' option. All right, so that prints Hello World on the console. All right my friends, so that's it for this, we'll take a short break here, and then in our next lesson we're going to, well, get into some more of the details of this Kotlin language. I hope you're enjoying it so far. And I'll see you in the next lesson.

 

About the Author
Students
47
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics