Save the text file. Now on the command line, run this command to compile it into an executable file:
kotlinc Hello.kt -include-runtime -d hello.jar
This will compile the “Hello.kt” file into a file called “hello.jar”. To execute that file, run this command:
java -jar hello.jar Hello World!
Congratulations! You’ve just successfully written your first Kotlin program and compiled it! Now that we know that Kotlin is working on our machine, let’s get started on writing some more code.
What's a Computer?
Before we get started working with Kotlin, it is helpful to learn a few fundamental concepts about how a computer works, especially with some basic data structures.
A computer is basically a machine that can store things in memory and rearrange its memory. It does this with its processor.
So, you can think of a computer as really just a machine (processor) that can put cute aliens into boxes pulls them out again (memory). You can only put one alien at a time though into each box.
In the first box, I’m going to put a blue alien. In the second I’ll put a green one. The third will hold a red one.
Now, my processor is a machine that puts these aliens into the boxes and can pull them out again. It can also do things like add the numbers together.
From Aliens to Kotlin
So how do we store something in Kotlin, like the number 1? Let’s create an alien that has the number 1. We can do this with the following Kotlin code:
var alien1 = 1
There we have it! We’ve just given the number 1 to our first alien!
Grouping Aliens Together
While putting individual items in individual variables is great, there are lots of times we want to store multiple items together. To do this we can create what is called an “array”. An array is a way to create a group of aliens and and then put stuff into them.
Here’s how we create an array in Kotlin and store the numbers 1, 2 and 3 in them:
var alien1 = arrayOf(1, 2, 3)
Let’s look at what’s going on here. We’re creating a group of aliens called “aliens”. We’re also storing the numbers 1, 2 and 3 in them. You may notice that we’re using arrayOf here. That just tells Kotlin that we want to create an array. Going back to our picture, we’re making three aliens and storing the numbers in them:
Now, when we create an array in the computer’s memory, there’s something you should know about what’s going on with the computer.
Computers have what is called linear memory. Linear memory is made up of a lot of aliens that come one right after the other. When we create an array, we need to tell the computer two things:
the number of aliens
the stuff we want to put into them
Note that Kotlin can automatically count how many aliens are needed by counting the number of things we tell it to create. Then the computer finds the necessary number of aliens and sets them aside. It also stores the values we give it into those aliens.
Whenever we want to get a specific value out of a alien, we can do the following:
aliens 1 <-- output
What is this aliens business? Well, that’s how we identify which alien in the array we want to change. First, the aliens array is the group of aliens we’re telling Kotlin we want to change. Next, the  part tells Kotlin which alien in the group we want to change. In this case, we’re changing the first alien. But why do we use 0? Well, a long time ago programmers decided they wanted to start counting from zero instead of one. So now, lots of programming languages do it this way. Silly, right? If we wanted to get the number stored in the second alien, we would use the number 1:
aliens 2 <-- output
So when you’re trying to figure out which alien you want to change, and you find yourself confused, just remember: whichever alien you want to change, subtract the number 1 from it and you’ll get the right one.
But what if we want to add another alien? With an array, we cannot do this. The reason has to do with how the computer stores information. When we told the computer to create an array of three aliens, the computer set those aside. However, the rest of the memory is up for grabs - any other group of aliens can be claimed.
Let’s say our computer only has 10 aliens available to use.
If we set aside the first 2, we only have 8 aliens left.
If we create another array storing 2 more aliens, we end up with something like this:
In order for the computer to keep track of the aliens in the array we have to keep them next to each other.
The computer knows where the first alien in the array begins, and it also knows how many it has set aside for the aliens.
But it needs to keep them all together, because it needs to know how to get to each one.
As long as it keeps them together, it’s fairly easy to find the right aliens that belong to the group, because all it has to do is move to the next one.
The processor knows that in order to go from the first alien to the second, it can just move to the next alien with a known position.
If it wants the last one, it knows the location of the first alien and it also knows the number of aliens, so it can just count how many aliens there are and then moves directly to the last alien.
Because we want to make sure the computer has a way to find the correct alien, we can’t add new aliens to that array because it would interfere with other arrays that might have been created as well. But there’s a solution to this problem! They’re called lists.
A list is another way to create a group of aliens and then put stuff into them. A list also allows us to group these aliens by a name so that we can easily keep stuff together. Let’s say we want to create a list (or group) of aliens with a single name and then store the numbers 1, 2 and 3 in them:
What’s going on here? So I’ve made a list of aliens called “aliens” and I’ve stored the values in them. What’s this mutableListOf thing? Well, that is how in Kotlin we create a list of aliens. If I wanted to change the number 1 to 4, I could do the following:
aliens = 4 [4, 2, 3] <-- output
Remember how with arrays we could not add new aliens once the array was created? Well, lists let us do that. If I want to add a alien containing 5 to our group of aliens, I can do this:
aliens.add(5) [4, 2, 3, 5] <-- output
Done! We now have our additional alien stored. But can we have aliens containing the same value? Let’s try adding another alien with the same 5 that we just added:
aliens.add(5) [4, 2, 3, 5, 5] <-- output
Voila! We can have the same number stored more than once!
But how does this magic work that allows us to add more aliens to our list? Remember when we said that an array needs to keep all aliens together so it can find them? Programmers created a way to eliminate the need for storing the aliens together so they can instead be scattered throughout the computer’s memory. This is exactly what a list allows us to do.
The way this works is with what are called nodes. A node typically has a value and a pointer to another node. To continue on our alien analogy, instead of a value occupying one alien, we create two attached boxes.
In the first box, we store the value alien. But in the second box, we point, or link, the location of the second node box in the list.
What this allows us to do then is to store any of the aliens anywhere in memory, as long as the pointers to other locations are arranged properly. All we need to remember is that for each value we store, we need two boxes, one to store an alien element, and one to point to the location of the node box containing the next value.
This means that we can add another alien to our list. The way this works is by finding out where the last value of the list is located. We can do this by following the links of the aliens from the beginning to the end. Once we have the last value, we can simply create two more aliens anywhere in memory, then store the new value in the first, and record its location in the second alien of the last value in our list.
We can also insert a new value in a list in the middle. We create two new aliens. Then we store the value of the first newly created alien in the second alien of the one that should come before it. Then the second of our two new aliens stores the location of the alien that we want to come after it.
I hope you enjoyed the first Kotlin Thursday! Join us next week for more! If you're interested in looking at previous Kotlin Thursdays, head over to our Github page.
Last but not least, this is a crowd-sourced tutorial. Do you have a topic you'd like to talk about? Reach out to us at email@example.com and we'll provide support for writing your lessons and editing your clips. To get started, take a look at our documentation.
Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.
When you create a profile for yourself you get a personal page automatically. That's where you can be creative and do your own thing. People who want to get to know you will click on your name or picture and…