Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
11: String Templates
How do I stop using the plus sign to glue strings together?
If you're coming from Java or C#, you're probably used to the "string concatenation" nightmare—using + every few words to inject a variable. It's tedious and makes the code hard to read. In Kotlin, we use String Templates. You just put a $ sign before the variable name directly inside the double quotes.
val characterName = "Sylas"
val weapon = "Void Blade"
// The old, clunky way:
println("Hero " + characterName + " wields the " + weapon + ".")
// The Kotlin way:
println("Hero $characterName wields the $weapon.")
I can't stress enough how much cleaner this is. It reads like a sentence rather than a puzzle.
When do I actually need the curly braces?
You'll see $variable and ${expression}. The rule of thumb is simple: if you're just referencing a single variable, the braces are optional. But the moment you need to access a property, call a method, or do a calculation, you need those braces to tell Kotlin where the expression ends and the string resumes.
class Player(val name: String, val health: Int)
val hero = Player("Sylas", 85)
// This won't work as you expect:
println("Health is $hero.health") // Prints: Health is Player@5f2108 and then ".health"
// Use braces for properties:
println("Health is ${hero.health}") // Prints: Health is 85
I usually just use the braces if I'm in doubt, but for simple local variables, omitting them is the standard style.
Can I put actual logic or math inside the template?
Yes, you can put almost any valid Kotlin expression inside ${}. This is incredibly powerful for quick formatting, like calculating a total or transforming a string on the fly.
val strength = 15
val weaponBonus = 5
println("Total Attack Power: ${strength + weaponBonus}")
println("Character Name in Uppercase: ${characterName.uppercase()}")
A word of advice: don't go overboard here. If you find yourself writing a complex if/else block or a long mathematical formula inside a string template, stop. Calculate the value in a separate variable first, then inject that variable. Otherwise, your code becomes a mess to debug.
What if I actually want to print a dollar sign?
This is the one "gotcha." Since $ is a reserved character for templates, you can't just type it if you're building something like a price tag. The cleanest way to handle this is to use a template that evaluates to a dollar sign literal.
val goldPieces = 150
println("Your balance is ${'$'}$goldPieces") // Prints: Your balance is $150
It looks a bit weird at first—a template inside a template—but it's the most reliable way to escape the symbol without breaking your string.
📋 Practical Task
Build an RPG Equipment Tooltip Generator
Your task is to create a small program that generates a formatted "tooltip" for a piece of gear in a game. You need to use string templates to handle both simple variables and expressions.
- Create three variables:
itemName(String),baseDamage(Int), andcritMultiplier(Double). - Create a final string called
tooltip. - The
tooltipstring must use templates to display the item name and the base damage. - Inside the same template, calculate and display the "Maximum Burst" (which is
baseDamagemultiplied bycritMultiplier). - Print the final
tooltipto the console.
Example Output: Item: Dragon Slayer | Base: 50 | Max Burst: 125.0
There are no comments for now.