Kotlin Basics: Kotlin 101

Start your coding journey with Kotlin

Kathryn Hodge
7 min readMay 26, 2017

Watch the video… or read the article.

Some Background

Google recently added Kotlin as an official programming language for Android. Before, Java was the only official language for Android, but now, developers can use Kotlin. Kotlin syntax is much more intuitive and even a little similar to Apple’s Swift language, but Google does not own Kotlin and the language will continue to be supported by JetBrains. Kotlin will also continue to target other platforms: the language is designed to run as native code on iOS and Macs, and also compiles to JavaScript for web development — so it’s a great language to know!

If you don’t know how to code, don’t worry! This tutorial assumes you have never coded before and Kotlin is your first experience with programming. Even if you have coded before, this should be a nice review of the basics! With a little bit of background, let’s get into the basics of Kotlin.

Download the Things

Before you can code in Kotlin, you’ll need to download either a text-editor (think Notepad, TextEdit, Sublime, Atom) or an IDE (Integrated Development Environment: specific software for writing code). Here, we’ll download the IntelliJ JetBrains IDE (Community Edition) as it gives us some great debugging features that you won’t get with your typical Sublime text-editor. If this is your first time coding in Java or Kotlin, then you will also need to download the JDK (Java Development Kit), which gives you some tools for running your applications.

Now that we have downloaded the Community Edition IntelliJ JetBrains IDE, we’ll go ahead and create our first Kotlin project. To do this, we’ll open IntelliJ and select Create New Project.

We’ll select Kotlin(Java) and click Next. If you don’t have the JDK, this is where you will need to download it.

Name the project whatever you like — here I’m naming it Hello. You can also click the three dots next to the Project location text field to change where the project is saved. When you are ready, click Finish.

Our first project is created, but now, we need to create a Kotlin file that will hold our code.

By clicking on the arrow next to Hello, we can see the src folder — this stands for source (as in source code) and it is where we will put our code.

Right clicking on src, we can hover over New and then select Kotlin File/Class.

For now, we’ll just call this file app and click OK.

Once you click OK, you should see this, which is our newly created Kotlin file.

1234 Let’s Code

Starting with the basics, we’ll create some global read-only variables (constants) with Kotlin. Here, read-only variables are noted with the keyword val, followed by the name, type, and value.

Above, we have create a constant with the name k, type Int, and value 4. We could name this constant anything we wanted, but here we name it k for kotlin. The type Int is short for Integer, which just means that k will stand for a whole number. The value 4 in the expressions means that whenever we use the k constant, we are really using its value, which is 4.

Think: If we had the expression k+k, it would really be 4+4, which would equal 8.

If we do not state the type, it is inferred by Kotlin. Here, we also write a comment with two slashes. A comment is just a little note for a developer looking at the code and does not affect the actual program. There is also a comment at the top that says who created this application and when it was created.

Above, we add another read-only variable with the name t, type double, and value 9.0.

Next, we’ll create some global mutable variables. Mutable variables can have their values changed, but constants or read-only variables can only have one set value — this will make more sense in a minute.

Here, we’ve created four mutable variables (l, i, n, and count). To create mutable variables, we use the keyword var, the name of the variable, (optional) the variable’s type, and its value.

With our read-only and mutable variables set, let’s use them! In order to use these, we can create something called a function. A function takes input, does some computation, and returns an output. You’ve been using functions all your lives — even if you didn’t know it! Adding two numbers is a function — you take two inputs (two numbers), add them, and then you have one number as your output. Let’s try coding a function that does the same thing, but for multiplication.

Here, fun is the keyword for function, followed by mult, the name of the function. Then, we have our two inputs (a and b), which are both integers, marked by the Int type. Lastly, we have :Int which says that the function will output or return a whole number (Integer).

The body / meat of the function is between the two curly braces. Here, we increment our global count variable and return their multiplication. What does a and b stand for? We’ll get to that. Right now, this function is only a definition — a blueprint saying that a function called mult will take two inputs and output the result of their multiplication. In order to bring this into action, we need to call this function from the main function. What is the main function?

The main function is a function named main that is automatically run when we run our program. Our mult function will not be run unless we specify it should by writing code inside the main function. Let’s go ahead and call our mult function inside the main method.

Here, we call the mult function with inputs 4 and 5. Going back to our function definition, a will have the value 4 and b will have the value 5. When we do a * b, we are really doing 4 * 5 and 20 will be returned from the function. Right now, we do nothing with that return value, but we could print it out to the console by writing println() around the mult function call.

To run our program, we can click on the green arrow in the top right corner. After running the program, we should get 20 in the console.

We could also print out the modified count variable with the following code.

To access the count variable, we write a dollar sign, an open curly bracket, and a closing curly bracket. We add these extra characters because we don’t want to print out “count” literally, but the value that the variable count evaluates to in the program.

The count variable will have the value one because we call the mult function one time during the program’s execution. Running the program, we see 20 and “Count is 1” in the console.

Fun Tip!

One last thing we can do is have a function with an inferred return type. If you have a short function, this can be a handy way to create it fast.

Here, we create a function named hi with one input called name that is of type String. In the body of the function, we access the name input to create the string “Hi Sally” or “Hi Taylor” or something else, depending on what the name input is. Calling this function in the main method, we write println(hi(“Kathryn”)).

And after running, we get “Hi Kathryn” in the console!

Now, you know some of the basic syntax for Kotlin. Mess around with it and try it out yourself with the JetBrains IDE. More tutorials on Kotlin and Android coming soon!

--

--