Let’s Learn How To Code — Part 000

Cody Engel
10 min readMay 27, 2017

--

You’re probably here because you want to learn how to write code, and I’m here because I want to attempt to help you. My goal for this series is to give you a solid foundation on how to write code, very akin to my very first computer science class. We will be writing code using the Kotlin programming language which can be used to create nearly any kind of application and is officially supported by Google for Android development.

Thanks Ilya Pavlov from Unsplash for this image. Side note: this is HTML/PHP, not Kotlin.

Before starting though I’d like to take a moment to give you a little background on myself. The first time I tried to learn how to code was in 2007 when I took an introduction to programming class during my senior year in high school. The assignments were in Visual Basic and I ended up failing the class and figured it just wasn’t for me. Then in 2010 I had some success with running blogs which ran on WordPress, so I decided I’d try learning again. I ended up ordering Beginning PHP 6, Apache, MySQL 6 Web Development which taught me about not only programming but also creating databases (MySQL). I built a few small web applications and decided to pursue a degree in Computer Science from Northern Illinois University. I started in January of 2011 and soon realized that I sucked at math which became a joke with my friends, since computer science is usually paired with math. Oh well.

I started to program professionally in 2013, in settings ranging from university, to start-ups, as well as large enterprises which employ tens of thousands of people. I started out with PHP/MySQL and today I work primarily with Android (Java/SQLite). I don’t want to say that everyone can learn how to code, it certainly is difficult. However considering I still suck at math, I like to think it’s not as hard as people make it out to be.

Let’s Start Coding In Kotlin

The first thing you need to do is download and setup IntelliJ IDEA. Go ahead and follow that tutorial and then come back to that article once you have completed the last step. If you run into any issues please let me know by commenting on this article and I’ll do my best to help you out.

Okay, so at this point you should have IntelliJ setup and were hopefully able to run your first app in Kotlin. Congratulations, I knew you could do it. So let’s start by creating our first Kotlin program together. We are going to expand on the Hello World example you likely just completed to allow for input from your keyboard as well as responding to that input.

Create A New Package

I love organization. So the first thing we are going to do to achieve this is create a new package. In slightly-less-techy words a package can be thought of as a folder on your computer, except instead of organizing pictures, it’s a way to organize your code.

In even less-techy words a package can be thought of as a moving box (also known as a real life package). When you are moving you might pack your kitchen in a series of boxes and those boxes may be labeled silverware, glassware, small appliances, pots/pans, and non-perishable food. This would be a great way of packing your moving boxes because when you need to unpack them in the future you know what to expect in each box which should make the process of moving easier.

So make use of packages to organize your code. In IntelliJ you can achieve this by right clicking on src, going to New, and selecting Package. Go ahead and name the package part_000.

Create A New Kotlin File

Now it’s time to start adding code into our package. For this we will need create a new Kotlin File. You can achieve this by right clicking on part_000, going to New, and selecting Kotlin File/Class. We’ll go ahead and name this file app, make sure that the Kind selected is File and click OK.

Make Kotlin Say Hello To You

Within your newly created Kotlin File we’ll start out by typing the following code.

fun main(args : Array<String>) {    // This is a comment in Kotlin, it's ignored by the computer
// but can be useful for humans trying to understand the code.
}

For the purpose of this article just assume that fun main(args : Array<String>) is magic. I promise I will explain what this is doing someday in the future but for right now just know that this is where your programs will run.

With that out of the way let’s figure out what the { is and what the significance is. A package is used for storing files and a file is where your code will be created. So the { is used for organizing similar pieces of code, you can also refer to this as logic. When you are done with that piece of logic you end it with a } to let the program know that it has reached the end of that logic.

The last piece is the text which started with // these are known as comments. They are completely ignored by the computer and are typically used for describing what your code is doing. It’s often useful to comment or document your code early on to describe what you’d like your program to do, and then go back and fill it in with the logic. So let’s go ahead and update our file to look more like this.

fun main(args : Array<String>) {

// Request the user for their name.

// Print out a message with their name and let them know we're
// going to start learning how to program in Kotlin.

}

Great, so now we have described what we expect our program to do. We want to ask for the user’s name and then print out a message with their name letting them know we’re going to start learning how to program in Kotlin. On a side note, if you are just copying + pasting this into your Kotlin File (also known as app.kt), please stop. The best way to learn is to actually type this out for yourself and pay attention to what your IDE (Integrated Development Environment) is doing. You’ll notice that it may try to autocomplete certain things for you or the text color may change based on your input. All of this will make your life so much easier in the future.

Alright, great. So the next thing we should probably do is fill in the logic to fulfill the first comment. If we do that we’ll end up with something like this.

fun main(args : Array<String>) {

// Request the user for their name.
val yourInput = Scanner(System.`in`)
System.out.print("Hey, who do I have the joy of speaking to? ")

// Print out a message with their name and let them know we're
// going to start learning how to program in Kotlin.

}

Alright so let me explain what both of those lines are doing in more detail. The first line is creating a variable named yourInput. Variables are one of the basic building blocks which are found in every program. The method for defining variables will differ from language to language and in Kotlin they are defined with the val keyword followed by the name followed by letting the program what it is equal to.

So with this line we’ve created a variable named yourInput which will take your input from the keyboard. For the purpose of this article don’t worry about what Scanner(System.'in') is doing, just know that it is what we are using to capture input from your keyboard.

You may have seen this, in the next paragraph I’ll explain why in was blue and System had a red squiggle.

One thing you may have noticed if you tried to type System.in instead is that IntelliJ highlighted the word in and gave you an option to select something called an InputStream, if you pressed enter you’ll then see that it added a tilde around the word in. The reason it did this was because in Kotlin the word in is known as a keyword which is reserved by the system, nothing else can be named after a keyword.

The next line is telling the system to output a message to the user. We’ll break down this a little further by first exploring what System.out.print(...) does. Essentially we are telling the system that we want to output something through printing it out on the screen. Anything within the parenthesis will be printed out to the user. In this case we are asking them who they are.

With that it is time to retrieve their name and print out a welcome message. Go ahead and update your code to look something like this.

fun main(args : Array<String>) {

// Request the user for their name.
val yourInput = Scanner(System.`in`)
System.out.print("Hey, who do I have the joy of speaking to? ")

// Print out a message with their name and let them know we're
// going to start learning how to program in Kotlin.
val yourName = yourInput.nextLine()
System.out.println("Hello $yourName, let's learn how to program with Kotlin :)")

}

Now before I explain what you just did go ahead and run the application and see for yourself. For your reference you can run the app by clicking on the icon to the right of fun main and selecting Run part_000.AppKt

It may take a moment to start. Essentially the computer is taking what you’ve typed in Kotlin and converting it into something that your computer can understand, this is known as compiling, you may hear programmers joke that they are going to go on lunch while they wait for their code compile; sadly sometimes it can take several minutes to several hours to compile very large applications. Don’t worry, the programs we are writing should never take more than a few seconds to compile.

So by this point you may have noticed the bottom of IntelliJ update with the following text: “Hey, who do I have the joy of speaking to?”. If you click into that section (this is known as a Terminal) and type out your name followed by pressing Enter you should notice that your application greets you and then terminates (finishes running). Below is an example of what I saw when I ran this on my computer.

So now that you’ve seen the program run it might make sense for me to explain what those two lines of code actually did. On the first line val yourName = yourInput.nextLine() we created a second variable called yourName. This variable is taking your input from the keyboard and once you pressed Enter it took that input and set it to yourName.

Neato torpedo, so what about the second line? It’s very similar to what we did to ask the user for their name except we are using the value stored by yourName to fill in the value dynamically.

System.out.println(“Hello $yourName, let’s learn how to program with Kotlin :)”)

You may have noticed that instead of print we are saying println instead. All this is doing is telling the system to print an entire line of data so that the next time the system prints something out, it will be on a new line. You also probably noticed that we are referencing yourName with a dollar sign between the quotation marks. This is how you can reference a variable in Kotlin from within a String of characters. With that I’ve realized that I failed to explain one of the most important aspects of our first program, and that is what the "" are. So I’ll go ahead and finish this article by explaining that.

What The Heck Are the Quotation Marks For?

One of the most popular kinds of variables in any program is known as a String. You can think of a String the same way you would think of text, because that is essentially what it is. You define a String by enclosing the text in quotation marks, for example if I wanted to store my name as a variable I might do something like this:

val codyEngel = "Cody Engel"

And that is all I have to teach you for this introduction. Congratulations, you just created your very first (well technically second) program in Kotlin!

So where do we go from here? If you are reading this shortly after this article was published you’re sort of stuck until I publish the next article. For the time being I strongly encourage you to play around with your program and see how you might modify it. You’re also more than welcome to click the heart icon on this article to let me know you enjoyed the article or leave a comment to let me know what you thought and if you had difficulty understanding anything in this article.

Ready for another lesson? Part 001 is now available.

--

--