Techbase Academy
Android · Lesson 06

Your First Program in Kotlin

8 phasesPersonal Introduction CardAndroid Studio
Phase 1 of 8
Lesson Introduction

Welcome to the first lesson of the Kotlin phase! Before you can build an Android app, you need to speak the language Android apps are written in. That language is Kotlin, and today you will write, run and understand your very first Kotlin programs.

Do not worry if you have never written a line of code. This lesson assumes nothing. If you can type on a keyboard and follow a recipe, you can do this. Programming is just writing very precise instructions for a computer, and a recipe is a good picture of that: "Boil the water, add the rice, wait ten minutes." A program is the same idea, except the cook is a computer that follows every instruction literally and never gets bored.

By the end of this lesson you will be able to:

  • Explain what Kotlin is and why Android developers use it.
  • Write and run a Kotlin program in the browser using the Kotlin Playground.
  • Read the anatomy of a program: fun, main, braces, parentheses and statements.
  • Print text with println and print.
  • Leave notes for humans with comments.
  • Insert values into text with string templates like $name and ${a + b}.
  • Run Kotlin code inside Android Studio.
  • Read an error message without panicking.

How this fits the bigger picture: This lesson matches the Your first program in Kotlin codelab in Unit 1 of Google's official Android Basics with Compose course. We go slower, explain more, and add extra practice. You will meet the official course page in the resources at the end.

Why Kotlin?

Android apps can be written in several languages, but since 2019 Google has been Kotlin-first: new Android APIs, documentation and tools are designed for Kotlin before anything else. Kotlin is concise (you write less code), safe (the compiler catches whole families of bugs) and friendly to beginners. Jetpack Compose, the modern way to build Android screens that you will use for most of this course, is built entirely around Kotlin.

✏️ Your Task
Practise what you just learned about Lesson Introduction. Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 2 of 8
Prerequisite Concepts

You need only four small ideas before you start. Each takes under two minutes.

1. What is a program?

A program is a list of instructions written in a programming language. A computer cannot understand English, so we write instructions in a language that is strict enough for a machine to follow exactly. Every instruction is called a statement. A program runs its statements from top to bottom, one after another.

2. What is source code?

The text you type is called source code. You save it in a file, and a tool turns that file into something the computer can actually run. For Kotlin, source files end in .kt.

3. What does a compiler do?

Computers run machine instructions, not Kotlin. A compiler is a translator that reads your Kotlin source code, checks it for mistakes, and produces instructions that can be run. When you build an Android app, the Kotlin compiler produces bytecode, and the Android build tools package it into an app that runs on the phone. This is why you can see errors before your program ever starts: the compiler found them while translating.

4. What is an IDE?

An IDE (integrated development environment) is a text editor with superpowers: it highlights your code in colours, suggests completions, spots mistakes as you type and has a button that runs your program. Android Studio, which you installed in lesson 02, is an IDE. Today we start with something even lighter, a website that runs Kotlin for you.

✏️ Your Task
Practise what you just learned about Prerequisite Concepts. Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 3 of 8
Conceptual Understanding: The Anatomy of a Kotlin Program

Look at the smallest useful Kotlin program:

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

This is only three lines, but every symbol has a job. Let us take it apart like a mechanic opening an engine.

The pieces

Piece What it is Real-world analogy
fun A keyword that means "I am defining a function". Writing "Recipe:" at the top of a page.
main The name of the function. Kotlin starts running your program here. The front door of a house.
() Parentheses hold the function's inputs. Empty here because main needs none. An empty order slip.
{ } Curly braces wrap the body: the statements that belong to this function. The recipe steps between the title and the end.
println("...") A built-in function that prints text and then moves to a new line. Reading a line out loud, then starting a new line.
"Hello, world!" A string: text wrapped in double quotes. The words you read out.

Key ideas to remember

  • main is the entry point. When you run a program, Kotlin looks for fun main() and starts there. No main, nothing runs.
  • Statements run top to bottom. The first statement finishes before the second begins.
  • Indentation is for humans. Kotlin does not care about spaces at the start of a line, but four spaces per level makes code readable. Android Studio does it for you; press Ctrl + Alt + L (Cmd + Option + L on a Mac) to reformat.
  • Semicolons are optional. Some languages need ; at the end of every line. Kotlin does not.
  • Case matters. println works, but Println and PRINTLN do not.

Think about it: What do you think happens if you write fun main() but forget the curly braces? (The compiler complains, because it cannot tell where the function begins and ends. You will see this error later.)

✏️ Your Task
Practise what you just learned about Conceptual Understanding: The Anatomy of a Kotlin Program. Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 4 of 8
Worked Examples in the Kotlin Playground

The fastest way to run Kotlin is the Kotlin Playground at play.kotlinlang.org. Open it in any browser, delete the sample code, and type the examples below. Press the green Run button and the output appears underneath. No installation is needed.

Example 1: Hello, world

Input (code):

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

Expected output:

Hello, world!

Explanation: main is called, println prints the string and ends the line, then the program finishes.

Example 2: Several statements in order

Input (code):

kotlin
fun main() {
 println("Welcome to Techbase Academy")
 println("Today we are learning Kotlin")
 println("Let's build Android apps!")
}

Expected output:

Welcome to Techbase Academy
Today we are learning Kotlin
Let's build Android apps!

Explanation: Three statements, three lines of output, in the same order you wrote them. Swap two lines and the output swaps too.

Example 3: print versus println

println means print line: it prints and then moves to a new line. print prints and stays on the same line.

Input (code):

kotlin
fun main() {
 print("Good ")
 print("morning, ")
 println("Ibadan!")
 println("The day has begun.")
}

Expected output:

Good morning, Ibadan!
The day has begun.

Explanation: The first two print calls stay on one line. The println finishes that line. The final println starts and ends a second line.

Example 4: Comments

A comment is a note for humans. The compiler ignores it completely.

kotlin
// This is a single-line comment
fun main() {
 println("Comments are ignored") // a comment can also follow code
 /* This is a multi-line comment.
       It can stretch across several lines. */
 println("Only the println lines run")
}

Expected output:

Comments are ignored
Only the println lines run

Why comments matter: Six months from now you will be the stranger reading your own code. Good comments explain why something is done, not what it obviously does. A useful trick: put // in front of a line to temporarily switch it off while you experiment. This is called commenting out. In Android Studio, select lines and press Ctrl + / (Cmd + / on Mac) to toggle comments.

Kotlin also has a third kind, a documentation comment that starts with /**. Tools use it to build help pop-ups. You will see it in library code.

Example 5: String templates

Programs become useful when they mix fixed text with changing values. Kotlin does this with string templates. Put a dollar sign $ before a name and Kotlin replaces it with that value.

kotlin
fun main() {
 val name = "Tunde"
 val age = 24
 println("My name is $name and I am $age years old.")
}

Expected output:

My name is Tunde and I am 24 years old.

You have not formally met val yet (lesson 07 explains it). For now, read val name = "Tunde" as "remember the text Tunde under the label name".

Example 6: Expressions inside templates

When you want to calculate something inside the text, wrap it in ${ }:

kotlin
fun main() {
 val price = 1500
 val quantity = 3
 println("Total: ₦${price * quantity}")
 println("Next year I will be ${24 + 1}.")
}

Expected output:

Total: ₦4500
Next year I will be 25.

Rule of thumb: a plain name needs only $name. Anything more complicated, such as a calculation or a function call, needs ${ ... }.

Example 7: Escape sequences

Some characters are awkward inside quotes. A backslash \ begins an escape sequence.

kotlin
fun main() {
 println("She said, \"Kotlin is fun!\"")
 println("Name:\tTunde")
 println("Line one\nLine two")
 println("Folder: C:\\Users\\Tunde")
 println("The price is \$5")
}

Expected output:

She said, "Kotlin is fun!"
Name: Tunde
Line one
Line two
Folder: C:\Users\Tunde
The price is $5

Sequence Meaning
\" a double quote inside a string
\n a new line
\t a tab (a wide gap)
\\ one real backslash
\$ a real dollar sign, not the start of a template

Example 8: Raw strings for multi-line text

Three double quotes create a raw string that can span many lines exactly as typed. trimIndent() removes the shared leading spaces.

kotlin
fun main() {
 val poem = """
        Roses are red,
        Kotlin is cool,
        Android apps
        Are what we rule.
    """.trimIndent()
 println(poem)
}

Expected output:

Roses are red,
Kotlin is cool,
Android apps
Are what we rule.

✏️ Your Task
Practise what you just learned about Worked Examples in the Kotlin Playground. Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 5 of 8
Running Kotlin Inside Android Studio

The Playground is perfect for learning, but you will spend your career in Android Studio, so let us run Kotlin there too. There are two reliable ways. Menu names occasionally move between Android Studio versions, so if something is not exactly where we say, look for the nearest matching item.

Option 1: A Kotlin library module

An Android app module cannot run a plain main function, because an app needs a phone or emulator to start. A Java or Kotlin Library module can, which makes it a perfect practice ground.

  1. Open any project (or create an Empty Activity project as you did in lesson 03).
  2. Choose File > New > New Module…, pick Java or Kotlin Library, name it practice, set the language to Kotlin, and click Finish.
  3. Wait for Gradle to sync. Open the practice/src/main/kotlin (or java) folder.
  4. Right-click the folder, choose New > Kotlin Class/File, name it Main, and pick File.
  5. Type your program:
kotlin
fun main() {
 println("Hello from Android Studio!")
}
  1. A small green triangle appears in the left gutter beside fun main(). Click it and choose Run 'MainKt'. The Run tool window opens at the bottom and shows your output.

Option 2: A scratch file

A scratch file is a throwaway file that never becomes part of your project.

  1. Choose File > New > Scratch File and pick Kotlin.
  2. Type a few lines such as println("Scratch works!").
  3. Click the green Run button at the top of the editor.

Scratch files are wonderful for quick experiments, like testing how a template behaves, without touching your real app.

Reading the Run window

The Run window shows your program's output, and when the program ends it prints something like Process finished with exit code 0. Exit code 0 means success. Anything else means something went wrong.

✏️ Your Task
Practise what you just learned about Running Kotlin Inside Android Studio. Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 6 of 8
Guided Practice Exercises
🎯 Your Challenge

Scenario: You run a small food stall called Mama Put and want a program that prints a tidy receipt for one customer.

Exercise 1: The Mama Put receipt

Objective: Combine println, string templates and a calculation.

Steps:

  1. Create val labels for the customer name ("Ada"), the plate price (1200) and the number of plates (3).
  2. Print a title line: === MAMA PUT ===.
  3. Print Customer: Ada using a template.
  4. Print Plates: 3 x ₦1200 using templates.
  5. Print Total: ₦3600 where the total is calculated inside a template, not typed in.
  6. Add a comment above your code explaining what the program does.
kotlin
// Prints a simple receipt for one customer at Mama Put
fun main() {
 val customer = "Ada"
 val price = 1200
 val plates = 3
 println("=== MAMA PUT ===")
 println("Customer: $customer")
 println("Plates: $plates x ₦$price")
 println("Total: ₦${price * plates}")
}

Expected output:

=== MAMA PUT ===
Customer: Ada
Plates: 3 x ₦1200
Total: ₦3600

Self-check question: Why does the total need ${ } but the customer name only needs $? (Because price * plates is an expression, not a single name.)

🎯 Your Challenge

Exercise 2: Fix the broken program

The program below has three mistakes. Find and fix them without running it first, then run it to check.

kotlin
fun Main() {
 println("Welcome to Android"
 println("Version: ${1 + 1")
}

There were three mistakes:

  1. Main has a capital M. Kotlin is case sensitive, so the entry point must be main.
  2. The first println is missing its closing parenthesis ).
  3. The template ${1 + 1 is missing its closing brace }.
kotlin
fun main() {
 println("Welcome to Android")
 println("Version: ${1 + 1}")
}

Expected output:

Welcome to Android
Version: 2

✏️ Your Task

Open the Playground and write a program that prints your name, your town and your favourite programming goal on three separate lines, using at least one string template. Then change the order of the lines and predict the output before you run it.

Phase 7 of 8
Common Beginner Mistakes (and How to Fix Them)

Every programmer has made every mistake on this page. Reading them now will save you hours.

Mistake #1: Forgetting the closing quote or parenthesis. The wrong code: println("Hello) The problem: the string never ends, so the compiler cannot find where it stops. The fix: every opening ", ( and { needs a matching closing one. Android Studio often adds the closing character for you, so type carefully and watch for red underlines.

Mistake #2: Wrong capital letters. The wrong code: Println("Hi") or Fun main(). The fix: Kotlin is case sensitive. Keywords like fun and function names like println are lowercase.

Mistake #3: Single quotes for text. The wrong code: println('Hello') The problem: single quotes are for a single character ('A'), not text. The fix: use double quotes for strings: println("Hello").

Mistake #4: Forgetting $ in a template. The wrong code: println("Hello name") The result: it prints the word name, not the value. The fix: println("Hello $name").

Mistake #5: A dollar sign you did not mean. The wrong code: println("Cost: $5") The fix: escape it with a backslash: println("Cost: \$5").

Mistake #6: Putting ${ } calculations outside quotes. The wrong code: println(${1 + 2}) The fix: templates only work inside a string: println("${1 + 2}"), or simply println(1 + 2).

How to read an error message

When something is wrong, the compiler prints a message like this:

text
Main.kt:3:5: error: unresolved reference: Println

Read it from left to right. Main.kt is the file, 3:5 is the line and column, and the text says what is wrong. Always start with the first error, because later errors are often just echoes of it. Errors are not failures. They are the compiler helping you.

✏️ Your Task
Practise what you just learned about Common Beginner Mistakes (and How to Fix Them). Open Android Studio, type the examples by hand, change a value, and observe what happens.
Phase 8 of 8
Knowledge Check

Answer each question in your head (or on paper), then reveal the answer.

Question 1. What is the entry point of every Kotlin program?

The main function: fun main() { ... }. Execution starts there.

Question 2. What is the difference between print and println?

print prints text and stays on the same line. println prints text and then moves to a new line.

Question 3. What does this print? val n = 4 then println("Double: ${n * 2}")

Double: 8. The expression n * 2 is calculated inside the template.

Question 4. Which is correct for printing a dollar sign: "\$" or "$$"?

"\$". The backslash escapes the dollar sign so it is not treated as the start of a template.

Question 5. Why can an Android app module not run a plain main function directly?

An Android app needs the Android runtime (an emulator or phone) to start. A plain main needs only a normal JVM, which is why we use a Kotlin library module or scratch file for practice.

Go deeper: official resources

✏️ Your Task
Practise what you just learned about Knowledge Check. Open Android Studio, type the examples by hand, change a value, and observe what happens.
🏗️ Build It — Mini Project
Personal Introduction Card

Project goal: Write a Kotlin program that prints a neat introduction card about you. This is your very first portfolio piece.

Requirements:

  1. Create val labels for your name, your age, your city, and a skill you are learning.
  2. Print a decorative top border using print or println (for example a line of = characters).
  3. Print at least five lines of information using string templates.
  4. Include one calculated line, such as the year you will turn 30, using ${ }.
  5. Add a comment at the top that names the program and says who wrote it.
  6. Use \t or \n at least once.

Stretch goal: Use a raw string (""") with trimIndent() to print a short biography across several lines.

Main.kt
// Personal Introduction Card
// Written by Tunde, Techbase Academy
fun main() {
 val name = "Tunde"
 val age = 24
 val city = "Ibadan"
 val skill = "Android development"
 val currentYear = 2026
 println("==============================")
 println("   PERSONAL INTRODUCTION")
 println("==============================")
 println("Name:\t$name")
 println("Age:\t$age")
 println("City:\t$city")
 println("Learning:\t$skill")
 println("I turn 30 in ${currentYear + (30 - age)}.")
 println("==============================")
 val bio = """
        I am learning to build apps
        that people in my community
        will actually use.
    """.trimIndent()
 println(bio)
}

Expected output:

==============================
PERSONAL INTRODUCTION
==============================
Name: Tunde
Age: 24
City: Ibadan
Learning: Android development
I turn 30 in 2032.
==============================
I am learning to build apps
that people in my community
will actually use.

1

Open your finished project in Android Studio. Check that the .gitignore file the wizard created is in the project root. It keeps local.properties, build folders and keystores out of Git. Never commit passwords or signing keys.

2

Choose Git > Create Git Repository… (or VCS > Enable Version Control Integration in older versions), select the project folder, then open Git > Commit, tick every file, and write the message “Add Android lesson 06 project”.

3

On github.com click New repository, name it android-lesson-06-your-first-program-in-kotlin, set it to Public, leave Add a README unticked, and click Create repository.

4

Back in Android Studio choose Git > Manage Remotes…, add the repository URL as origin, then choose Git > Push… and push your main branch. Prefer the terminal? Run git remote add origin <your-url> then git push -u origin main.

5

Add a README.md with a screenshot of your app running and a short description. Employers and clients read READMEs first. Your project is now part of your public portfolio. 🎉

Lesson 06 complete! 🎉

You covered:

  • Lesson Introduction
  • Prerequisite Concepts
  • Conceptual Understanding: The Anatomy of a Kotlin Program
  • Worked Examples in the Kotlin Playground
  • Running Kotlin Inside Android Studio
  • Guided Practice Exercises
  • Common Beginner Mistakes (and How to Fix Them)
  • Knowledge Check
All 72 lessons in Google Android Studio ·Techbase Academy ·© 2025–2026 Techbase Consultant Services, Ibadan
Esc
↑ ↓ to moveEnter to openEsc to close