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
93: IntRange, CharRange, and Progressions
I've noticed a recurring pattern when developers move from languages like Python or JavaScript to Kotlin: they assume that writing 1..100 actually creates a list containing one hundred integers in memory. It feels intuitive, right? You see a range, you think "collection."
But that's a costly misconception. Let's look at why that's wrong:
val hugeRange = 1..1_000_000
// You might think this just allocated 4MB of RAM for a million Ints.
// In reality, it allocated one single object that just stores two numbers: 1 and 1,000,000.
If ranges were actually lists, your memory usage would spike every time you wrote a simple for loop. Instead, Kotlin uses Progressions. A range is just a blueprint; it knows where it starts and where it ends, and it calculates the "next" value on the fly only when you actually ask for it.
Ranges are boundaries, not pre-filled lists
Because an IntRange or a CharRange is just a set of boundaries, they are incredibly efficient for checks. I almost always prefer the in operator over writing clunky comparison logic.
Take a scenario where you're validating a user's age for a specific movie rating. Instead of writing if (age >= 13 && age <= 17), you can just do this:
val age = 15
if (age in 13..17) {
println("You're in the teen bracket.")
}
This works for characters too. Since characters are essentially numeric values under the hood, CharRange allows you to check for categories without a massive when block. If I want to see if a character is an uppercase letter, I don't need a regex; I just check the range:
val char = 'G'
if (char in 'A'..'Z') {
println("It's an uppercase letter!")
}
Controlling the flow with step and downTo
The standard .. operator is great, but it only moves forward by one. In the real world, you often need more control over how you traverse a range. This is where step and downTo come in.
If you try to use 10..1, Kotlin won't throw an error, but the loop simply won't execute. The .. operator expects the start to be less than or equal to the end. To count backwards, you must use downTo.
Here is a practical example. Imagine you're building a simple countdown for a rocket launch, but you only want to announce every second second to avoid cluttering the logs:
for (i in 10 downTo 1 step 2) {
println("T-minus $i seconds...")
}
// Output:
// T-minus 10 seconds...
// T-minus 8 seconds...
// T-minus 6 seconds...
// T-minus 4 seconds...
// T-minus 2 seconds...
I like to think of step as a modifier for the progression's "stride." You can apply it to both increasing ranges and downTo progressions. Just keep in mind that the step value must be positive; if you try to use a negative step, the compiler will stop you.
📋 Practical Task
Exercise: The Security System Access Validator
You are building a security module for a high-tech vault. The vault has three security tiers based on a numeric "Clearance Level" (1 to 100) and a specific "Access Code Character" (A to Z).
Write a program that does the following:
- Define a variable
clearanceLeveland a variableaccessCode. - Use a range check to determine the tier:
- 1..30: "Low Clearance"
- 31..70: "Mid Clearance"
- 71..100: "High Clearance"
- Anything else: "Invalid Clearance"
- Check if the
accessCodeis a valid uppercase letter using aCharRange. If it's not, print "Invalid Access Code". - If the user has "High Clearance" and a valid access code, simulate a "Vault Unlocking" sequence by printing a countdown from 5 down to 1, but only printing every odd number (5, 3, 1) using a progression.
There are no comments for now.