LANGUAGE OVERVIEW

Kotlin Makes
Coding Simple.

Kotlin is a tool used to build apps. It was built to fix the problems with older languages like Java. It is safer, faster to write, and is the number one choice for building Android phone apps.

Read The Full Breakdown
v1.9.0

Short Code

Write less lines to do the same job.

No Crashes

Built-in rules stop apps from breaking.

Android Native

The official language for Android phones.

1. Why was it made?

For a long time, Java was the main tool for building Android apps. But Java is old. It was made in the 1990s.

Using Java takes a lot of typing. You have to write many words just to do simple things. Programmers call this "Boilerplate code". It means wasted typing.

Kotlin was created by a company called JetBrains. They wanted a tool that does everything Java does, but with less typing and fewer mistakes.

The Java Problem

  • Too many words to set up simple data.
  • Easy to make a mistake that crashes the app.
  • Hard to read when the app gets very large.

2. Less Typing, More Doing

Kotlin cuts out the extra words. When you type less, you make fewer mistakes. It is also much easier for other people to read your work. Let's look at how you store a simple piece of information, like a user's name and age.

JAVA (The Old Way) 14 Lines
public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
KOTLIN (The New Way) 1 Line
data class User( val name: String, val age: Int ) // That's it. It does the exact same thing.

3. Stopping The Big Crashes

In the past, the biggest reason phone apps crashed was because they looked for data that was not there. Programmers call empty data "Null".

If Java looks for a name, and the name is "Null" (empty), the whole app stops working and closes. This is famous. It is called the "Billion Dollar Mistake" because it has cost companies so much time and money to fix.

Kotlin fixes this by forcing you to make a rule. When you write the code, Kotlin asks: "Is it possible for this data to be empty?"

If you say yes, Kotlin makes you write a backup plan right then and there. The app will not even start until you make a backup plan.

The Crash Guard (?)

In Kotlin, you put a question mark next to data that might be empty.

var name: String
// NEVER allowed to be empty
var name: String?
// CAN be empty. App will check first.

4. The Quick Backup Plan

We just learned that Kotlin makes you handle empty data. But typing out a long "if this is empty, do this instead" takes too much time.

Kotlin has a shortcut called the Elvis Operator. It looks like a face with hair: ?:

It simply means: "Try to use the first thing. If it is empty, use the second thing instead."

How it works:

// Try to get the user's name.
// If they have no name, call them "Guest".

val displayName = userName ?: "Guest"

5. Text With Memory

In old languages, mixing words and data is messy. You have to use lots of plus signs (`+`) and quote marks (`""`) to glue them together. Kotlin uses String Templates to make it as easy as writing a normal sentence.

The Messy Old Way
"Welcome, " + name + "! You have " + count + " new messages."
The Clean Kotlin Way
"Welcome, $name! You have $count new messages."

Just put a dollar sign next to the data, right inside the sentence.

6. Skipping The Blanks

Imagine you have a machine that makes coffee. Usually, you want regular milk and no sugar. In Java, you have to build three different machines: one that takes all details, one that assumes regular milk, and one that assumes no sugar.

Kotlin has Default Arguments. You build one machine, and you tell it what the normal choices are. If the person ordering doesn't mention milk, the machine automatically uses regular milk.

One Setup, Many Uses

// Setup defaults
fun makeCoffee(
  milk: String = "Regular",
  sugar: Int = 0
)
makeCoffee()
// Gets Regular milk, 0 sugar
makeCoffee(sugar = 2)
// Gets Regular milk, 2 sugar

7. The Smarter Menu

When you have many choices to make (like "If red do this, if green do this, if blue do this"), writing lots of "If" statements gets confusing. Kotlin uses a tool called When. It reads like a clean, simple restaurant menu.

What should the car do?
when (light) {
"Red" stopCar()
"Yellow" slowDown()
"Green" drive()
else turnOnFlashers()
}

8. Passing Actions Like Data

Usually, you pass data into a function. Like handing a blender an apple.

But what if you want to hand the blender a new instruction, like "blend slowly today"? In Kotlin, you can package up an action (a block of code) and pass it around exactly like data.

These packaged actions are called Lambdas. They are the secret to why clicking buttons in modern Android apps is so easy to code.

Button
{ Action }
myButton.onClick {
    // This action waits inside the button
    // until someone clicks it.
    playSound()
}

9. Supercharged Lists

Finding specific items in a long list used to require writing messy "loops" that check every single item manually. Kotlin gives you powerful tools to sort, filter, and change lists with just one line of code.
All Users
[ "Anna, 25",
"Bob, 17",
"Cara, 30" ]
.filter { it.age >= 18 }
Adults Only
[ "Anna, 25",
"Cara, 30" ]

10. Don't Work Until You Have To

The "Lazy" Keyword

It keeps heavy tasks asleep until the exact moment they are needed.

val hugeDatabase by lazy {
  loadData() // Sleeps until used
}

Some parts of an app take a lot of power to set up. Like loading a map or opening a huge list of pictures.

If you load everything as soon as the app opens, the app will start very slowly. Kotlin lets you mark data as Lazy.

When data is lazy, the app prepares it, but doesn't actually load it into memory until the user clicks the button to look at it. This saves memory and makes apps start instantly.

11. Closed Menus (Sealed Classes)

When an app talks to the internet, only three things can happen: It is Loading, it Succeeds, or it gets an Error. You do not want random, unexpected things to happen.

Kotlin has Sealed Classes. Think of them as a closed, locked box of choices. You tell the app: "These are the only 3 possible states. No one can ever add a 4th state."

Because the app absolutely knows there are only 3 choices, it forces you to write code for all 3. If you forget to write what happens during an Error, the app will refuse to run.

Network State
  • Loading
  • Success
  • Error
No other choices allowed.

12. The Focus Box

Sometimes you need to do 5 or 6 things to the exact same object. Writing the object's name over and over is annoying. Kotlin gives you "Scope Functions" (like apply or with) to put a focus box around an object.

Without Focus Box

val button = Button() button.color = "Red" button.text = "Click Me" button.size = "Large" button.show()

With Kotlin 'apply'

val button = Button().apply { color = "Red" text = "Click Me" size = "Large" show() }

13. Working Together Smoothly

Companies have millions of lines of Java code. They cannot just throw it away. The best thing about Kotlin is that it understands Java perfectly. You can use both in the same project. Programmers call this "Interoperability".

JAVA FILE

Old code kept safe and running.

100% Shared
KOTLIN FILE

New code built fast and easy.

This means a company can add just one new screen in Kotlin to test it out, without breaking the rest of their older Java app.

14. Google's Favorite Choice

Kotlin-First

In 2019, Google announced that Android development will be "Kotlin-First".

This means if Google makes a new tool, a new guide, or a new feature for Android phones, they build it for Kotlin before they build it for anything else.

Why did Google do this? Because developers were much happier using Kotlin.

They found that apps written in Kotlin were built faster and crashed much less. It was better for the people making the apps, and better for the people using the apps.

Today, almost all professional Android jobs require you to know Kotlin. Java is now mostly used just to fix old apps.

15. Doing Many Things at Once

The Freeze Problem

Have you ever used an app, pressed a button, and the whole screen froze for a few seconds? This happens when the app is trying to load heavy data (like an image from the internet) on the main screen.

To fix this, programmers have to tell the app to load heavy data in the "background". In old languages, doing this was very difficult and messy.

Kotlin has a feature called Coroutines. Think of Coroutines as magic workers in a restaurant kitchen.

Instead of the waiter (the main screen) cooking the food and ignoring the customers, the waiter hands the order to a Coroutine (the chef). The waiter keeps helping customers, and the Coroutine gives the food back when it is ready. No freezing. No waiting.

16. Adding New Tricks

Sometimes you are using a tool built by someone else, and you wish it had one more button or feature. You cannot change their code because you do not own it. Kotlin fixes this with Extension Functions.

Extension functions let you stick a brand new trick onto an existing tool, without changing the tool itself.

// Let's say you want to easily remove spaces from any word.

fun String.removeSpaces(): String {
    return this.replace(" ", "")
}

// Now EVERY word in your app has this new trick!
val cleanWord = "Hello World".removeSpaces() // Becomes "HelloWorld"

17. The Code is Smart

In older languages, even if you check what type of data you have, you have to keep reminding the computer over and over.

For example: "Is this a word? Yes it is. Okay, treat it like a word."

Kotlin has Smart Casts. It has a memory. Once you check something, Kotlin remembers it for the rest of that section. It stops you from having to write the same checks again and again.

// Kotlin remembers!
if (item is String) {
    // Kotlin now KNOWS this is a word.
    // You can immediately check its length.
    print(item.length)
}

18. Not Just For Phones

Kotlin became famous because of Android phones. But it is so easy to use that people started wanting it everywhere. Today, Kotlin is a "Multiplatform" tool.

Backend Servers
Websites
Apple iOS
Desktop Apps

19. Quick Summary

Why switch to Kotlin?

You write much less code.
Apps crash less often.
It works perfectly with old Java code.
Google demands it for Android.
It does heavy background work easily.
You can use it for servers and websites too.