Write your first program in Kotlin

1. Before you begin

In this codelab, you are going to write your first program in the Kotlin language using an interactive editor that you can run from your browser.

You can think of a program as a series of instructions for the system to perform some action. For example, you could write a program that creates a birthday card. In that program, you could write an instruction to print congratulatory text or calculate someone's age from their birth year.

Just like you use human language to communicate with another person, you use a programming language to communicate with the operating system of your computer. Fortunately, programming languages are less complex than human languages and quite logical!

Android apps are written in the Kotlin programming language. Kotlin is a modern language created to help developers write code efficiently and with as few errors as possible.

Learning to create an app and learning the basics of programming at the same time will be challenging, so we are going to start you off with a bit of programming before getting into app creation. Becoming comfortable with some programming basics first is not only an important step towards creating apps, it is also going to make it easier to create your first app later in this course.

Code editors are tools that help you write code, in the same way a word processor (like Google Docs) helps you create text documents. In this codelab, you are using an interactive Kotlin editor within your browser. This means that you do not have to install any software to take your first step towards app development.

Prerequisites

  • Use interactive websites in your web browser.

What you'll learn

  • How to create, change, understand, and run a minimal Kotlin program that displays a message.

What you'll build

  • A program in the Kotlin programming language that displays a message when you run it.

What you need

  • A computer with a modern web browser, such as the latest version of Chrome.
  • Internet access for your computer.

2. Run your first program in Kotlin

In this task, you will use an editor on a website to start programming in the Kotlin language right away.

Use an interactive code editor

Instead of installing software on your computer, you can use a web-based tool to create your first program.

  1. In your browser, open https://developer.android.com/training/kotlinplayground. This opens a browser-based programming tool.
  2. You should see a page similar to the screenshot below, with a code editor in the middle. Kotlin Playground

This is the program code in the editor:

fun main() {
    println("Hello, world!")
}

Run the program code

Running a program that you created is not much different than running a program such as a word processor on your computer. The difference is that when you run a program to accomplish a task, or play a game, you primarily care about what the program can do for you, and you don't concern yourself with the code that makes it work. When you are programming, you get to see and work with the actual code that makes the magic happen.

Let's see what this program does!

  1. In the editor, in the top-right corner, find the white or green triangle 63ca117bafffc8da.png and click it to run the program.
  2. Look at the pane at the bottom.
Hello, world!
  1. Notice Hello, world! printed, like in the image above. So now you know what this program does: It prints, or outputs, a hello world message.

Compilation is a process that translates the Kotlin program code into a form that the system can run. If compilation completes successfully, there are no errors in the program that would keep it from running. If there are problems, they will appear in the pane at the bottom.

3. Modify your program

Change the Hello World code

Let's change the program to make it do something a little different.

  1. Change the "Hello, world!" text to say "Happy Birthday!".
  2. Run your program by clicking the blue or green run button at the top right.
  3. At the bottom, you should now see Happy Birthday! printed, as shown below.
Happy Birthday!

How does it work?

How is this done? This seems like a lot of code to just print something!

Well, if you wanted a friend to write "Hello, world!" on a piece of paper, there is a lot of implied information. If you just tell them, "Write ‘Hello world!' on this piece of paper", they are going to make assumptions about the information you left out. For example, they are going to assume they need to use a pen, and that you want them to write it using letters! The computer does not make these assumptions, so you have to give precise instructions that include every step.

Just like the English language has structure, so does a programming language. If you've ever learned another language, you know the challenge of learning the grammar, the spelling, perhaps a new alphabet of symbols, and the vocabulary. Learning to program has similar challenges, but fortunately, it is less complex and a lot more logical than learning, for example English.

Understand the parts of the program

Now, take a look at the code. Each piece of this program serves a specific purpose, and you need all the pieces in order to be able to run the program. Let's start with the first word.

fun
  • fun is a word in the Kotlin programming language. fun stands for function. A function is a section of a program that performs a specific task.
fun main
  • main is the name of this function. Functions have names, so they can be distinguished from each other. This function is called main, because it is the first, or main, function that is called when you run the program. Every Kotlin program needs a function named main.
fun main()
  • The function name is always followed by () two parentheses.
  • Inside the parentheses, you can put information for the function to use. This input to the function is called "arguments" or args for short. You will learn more about arguments later.
fun main() {}
  • Notice the pair of curly braces {} after the parentheses. Inside a function is code that accomplishes a task. These curly braces surround those lines of code.

Look at the line of code between the curly braces:

println("Happy Birthday!")

This line of code prints the Happy Birthday! text.

  • println tells the system to print a line of text.
  • Inside the parentheses you put the text to be printed.
  • Notice that the text to be printed is surrounded by quotes. This tells the system that everything inside the quotation marks should be printed exactly as given.

To actually print the text, this whole println instruction has to be inside the main function.

So, there it is. The smallest Kotlin program.

fun main() {
    println("Happy Birthday!")
}

4. Extend your program

Great job! You printed one line of text using the println() function. However, you can put as many lines of instructions inside a function as you want or need to get a task accomplished.

  1. Copy the line println("Happy Birthday!") and paste it two more times below it. Make sure your pasted lines are inside the curly braces of the main function.
  2. Change one text to be printed to someone's name, say "Jhansi".
  3. Change the other text to be printed to "You are 25!".

Your code should look like the code below.

fun main() {
    println("Happy Birthday!")
    println("Jhansi")
    println("You are 25!")
}

What would you expect this code to do when it runs?

  1. Run your program to see what it does.
  2. Go to the output pane, and you should see 3 lines printed in the console window, as shown below.
Happy Birthday!
Jhansi
You are 25!

Nice work!

Dealing with errors

Making mistakes while programming is normal, and most tools will give you feedback to help you fix mistakes. In this step, create a mistake to see what happens.

  1. In your program, remove the quotes around the text Jhansi, so that line looks as shown below.
println(Jhansi)
  1. Run your program. You should see Jhansi printed in red, and an exclamation mark next to the line of code you changed, to show you where there is an error. Message with error exclamation mark
  2. Look at the output pane. It shows a message with the same exclamation mark icon. What follows is a description of the error in your code.

Message: Unresolved reference

  1. This message, Unresolved reference: Jhansi, tells you what the system thinks is the error in the code. Even if you don't know what the error message means, you may be able to figure out what's wrong. In this case, you know that the println() instruction prints text. You learned earlier that the text has to be between quotes. If the text is not quoted, that is an error.
  2. Go ahead and add the quotes back in.
  3. Run your program to make sure it works again.

Congratulations, you have run and changed your first Kotlin program!

5. Solution code

This is the complete code of the program you worked on in this codelab.

fun main() {
    println("Happy Birthday!")
    println("Jhansi")
    println("You are 25!")
}

6. Summary

  • https://developer.android.com/training/kotlinplayground is an interactive code editor on the web where you can practice writing Kotlin programs.
  • All Kotlin programs need to have a main() function: fun main() {}
  • Use the println() function to print a line of text.
  • Place text you want to print between double quotes. For example "Hello".
  • Repeat the println() instruction to print multiple lines of text.
  • Errors are marked red in the program. There is an error message in the output pane to help you figure out where the error is and what might be causing it.

7. Learn more

8. Practice on your own

Do the following:

  1. Change the println() instructions to print().
  2. Run your program. What happens?

Hint: The print() instruction just prints the text without adding a line break at the end of each string.

  1. Fix the text so that each part of the message is on its own line.

Hint: Use \n inside the text to add a line break. For example, "line \n break". Adding a line break changes the output as shown below.

Hint: You can print an empty line by supplying no text: println("").

Code:

fun main() {
    println("no line break")
    println("")
    println("with line \n break")
}

Output:

no line break

with line 
 break

Check your work:

Here is one possible solution:

fun main() {
    print("Happy Birthday!\n")
    print("Jhansi\n")
    print("You are 25!")
}