Codetown ::: a software developer's community
Welcome to the first Kotlin Thursday of the season! We're glad to have you here.
This week, we're going to look into getting started with Kotlin and reviewing some fundamental concepts in programming. Let's get started!
Think of these resources as supplemental if you happen to be more curious. We always encourage looking into documentation for things you use!
Before we can begin working with Kotlin, you’ll need to set up Kotlin on your computer.
However, if you’d like to just try Kotlin out without having to install anything on your computer, you can try Kotlin in the browser: https://play.kotlinlang.org
Here’s a hello world example: https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20ve...
If you'd like to install a repl on your own computer:
Kotlin requires Java to be installed. To do this on a Mac:
brew cask install java8
Next, you will need to install the Kotlin compiler. Open your terminal and run the following command:
brew install kotlin
Let’s test if our Kotlin compiler works, by creating a startup program.. Create a text file named “Hello.kt”. Open it in your favorite text editor and add the following code:
package Hello
fun main(args: Arrayspan style="color: #d73a49;">String>) {
println("Hello World!")
}
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.
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.
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!
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:
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[0]
1
<-- outputWhat is this aliens[0] 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 [0] 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[1]
2
<-- outputSo 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:
var aliens = mutableListOf(1, 2, 3)
[1, 2, 3]
<-- outputWhat’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[0] = 4
[4, 2, 3]
<-- outputRemember 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]
<-- outputDone! 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]
<-- outputVoila! 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 kthursdays@gmail.com and we'll provide support for writing your lessons and editing your clips. To get started, take a look at our documentation.
Tags:
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.
Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.
Check out the Codetown Jobs group.
AWS has launched the open-source Model Context Protocol (MCP) Servers, revolutionizing AI-powered code assistants. These servers enhance development speed and security, ensuring adherence to AWS best practices. With features like automated Infrastructure as Code and cost insights, MCP democratizes AWS expertise and empowers developers to optimize cloud solutions effortlessly.
By Steef-Jan WiggersAWS has recently announced the general availability of Amazon VPC Route Server. This new option simplifies dynamic routing in a VPC, allowing developers to advertise routing information via Border Gateway Protocol (BGP) from virtual appliances and dynamically update the VPC route tables associated with subnets and internet gateways.
By Renato LosioIan Hoffman discusses Slack's architectural evolution from workspace-centric to Unified Grid. He explains scaling challenges & Enterprise Grid complexities, and shares lessons learned during this significant architectural shift, drawing insightful parallels to the history of astronomy and emphasizing the importance of questioning foundational assumptions in software development.
By Ian HoffmanIn this podcast, Shane Hastie, Lead Editor for Culture & Methods, spoke with Trisha Gee about the challenges and importance of addressing flaky tests, their impact on developer productivity and morale, best practices for testing, and broader concepts of measuring and improving developer productivity.
By Trisha GeeStefania Chaplin and Azhir Mahmood explain how to navigate the complexities of AI in highly regulated industries. They discuss MLOps pipelines, data security, evolving legislation (GDPR, EU AI Act), and the critical frameworks for responsible, secure, and explainable AI. Learn practical prevention techniques, XAI methods, and future trends in AI for cybersecurity and beyond.
By Stefania Chaplin, Azhir Mahmood
© 2025 Created by Michael Levin.
Powered by