Kotlin: Modern App Development
The concise and safe JVM language.
The preferred language for modern Android development and enterprise backends.
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 BreakdownShort 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.
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.
// NEVER allowed to be empty
// 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:
// 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.
"Welcome, " + name + "! You have " + count + " new messages."
"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
fun makeCoffee(
milk: String = "Regular",
sugar: Int = 0
)
// Gets Regular milk, 0 sugar
// 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.
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.
myButton.onClick {
// This action waits inside the button
// until someone clicks it.
playSound()
}
9. Supercharged Lists
"Bob, 17",
"Cara, 30" ]
"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.
- Loading
- Success
- Error
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".
Old code kept safe and running.
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.
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 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.
19. Quick Summary
Why switch to Kotlin?
More in this series
Master more skills with other tutorials from the Mobile Development series.
Data Management
- T-SQL: Transact-SQL: Microsoft's proprietary extension to SQL.
- PL/SQL: Oracle's Database Language: Procedural extensions to SQL.
- SQL: Structured Query Language: Manage and query relational data.
Systems Programming
- V: Simple, Fast, Safe: Compiled language for maintainable software.
- Nim: Efficient, Expressive, Elegant: Compiled systems programming language.
- Crystal: Fast as C, Slick as Ruby: High performance with beautiful syntax.
- Carbon: An Experimental Successor to C++: Performance with modern language features.
- Zig: Modern Systems Programming Language: Performance, safety, and simplicity.
- Assembly: Low-Level Machine Code: Direct hardware control and performance.
- Ada: Reliable Systems Programming: Safety and concurrency for critical systems.
- Haskell: Purely Functional Excellence: The gold standard of functional programming.
- OCaml: Pragmatic Functional Programming: Speed, safety, and expressive power.
- Erlang: The Language of Reliability: Building systems that never sleep.
- Rust: Safe Systems Programming: Performance without the fear.
- C++: High-Performance Engineering: The language of performance and control.
- C: The Foundations of Computing: Understanding the machine at its core.
Data Science
- Mojo: AI Programming Language: Python usability with C performance.
- R: Statistics and Data Analysis: Statistical computing and graphics.
- Julia: High-Performance Scientific Computing: Fast, dynamic language for numerical analysis.
Scientific Computing
- MATLAB: Engineering and Numerical Computing: Powerful tools for engineers and scientists.
Graphics Programming
- HLSL: DirectX Shading Language: Write shaders for DirectX graphics.
- GLSL: Graphics Shading Language: Write shaders for real-time graphics.
Game Development
- GDScript: Godot Engine Scripting: Rapid game development with Godot.
Hardware Design
- VHDL: Very High-Speed Integrated Circuit HDL: Describe and simulate digital systems.
- Verilog: Hardware Description Language: Design digital circuits and FPGAs.
Enterprise & Backend
- F#: Succinct and Robust .NET Logic: Functional-first programming for the modern enterprise.
- Scala: Scalable Software Engineering: Merging Object-Oriented and Functional paradigms.
- Go: Built for the Cloud: Reliable, simple, and incredibly fast.
- C#: Modern Enterprise Logic: The backbone of the .NET ecosystem.
- Java: Enterprise-Grade Logic: The foundation of robust software engineering.
Web Development
- Elixir: Reliable and Scalable Logic: Building high-performance, distributed applications.
- Ruby: Designer's Favorite: A language for developer happiness.
- PHP: The Web's Engine: Powering the majority of the internet.
- TypeScript: Type-Safe Programming: Building scalable JavaScript applications.
- JavaScript: The Language of the Web: Powering interactive user experiences.
Mobile Development
- Swift: The Future of Apple Apps: Fast, safe, and modern.
- Kotlin: Modern App Development: The concise and safe JVM language.
- Dart: Client-Optimized Logic: Fast apps on any platform.
Scripting & Automation
- PowerShell: Windows Automation: The ultimate tool for IT professionals.
- Perl: The Swiss Army Chainsaw: Flexible and powerful text processing.
- Bash: The Power of Automation: Make your computer do the hard work.
- Python: Versatile Programming: Simplicity and power for every use case.