0Pricing
Kotlin Academy · Lesson

Char and Custom Ranges

Beyond integers.

Char and Custom Ranges is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond Integers

Ranges are not just for Int. Kotlin supports ranges of characters and other comparable types.

This lesson explores char ranges and how custom types can join in with rangeTo.

Char Ranges Recap

Characters have a defined order, so 'a'..'z' is a valid range.

You can iterate, check membership, and more.

fun main() {
    for (c in 'a'..'f') print(c)
    println()
    println('q' in 'a'..'z')
}

Validating Characters

Char ranges make input validation readable, such as checking for digits or letters.

fun isDigit(c: Char): Boolean = c in '0'..'9'

fun main() {
    println(isDigit('7'))
    println(isDigit('x'))
}

Counting Letters

You can build a quick alphabet by iterating a char range and collecting the values.

fun main() {
    val alphabet = ('a'..'z').toList()
    println(alphabet.size)
    println(alphabet.first())
    println(alphabet.last())
}

Long Ranges

Ranges work for Long too, useful for large numeric spans.

The same operators apply.

fun main() {
    val big = 1L..5L
    println(big.sum())
}

Comparable Ranges

Any Comparable type can form a range with .., though such ranges only support membership checks, not iteration.

For example, String ranges check lexical order.

fun main() {
    val range = "apple".."mango"
    println("cherry" in range)
    println("zebra" in range)
}

Why Some Ranges Cannot Iterate

Iterating needs a defined step. Integers and chars have one (add 1), but strings and doubles do not have an obvious next value.

So those ranges support in but not for loops.

fun main() {
    val r = 1.0..5.0
    println(2.5 in r)
    // for (x in r) // not allowed for Double
}

Custom Types with rangeTo

The .. operator calls a rangeTo function. If your class implements Comparable, you get range membership for free.

Here a simple version class compares numerically.

data class Ver(val n: Int) : Comparable<Ver> {
    override fun compareTo(other: Ver) = n - other.n
}

fun main() {
    val range = Ver(1)..Ver(5)
    println(Ver(3) in range)
    println(Ver(9) in range)
}

Operator Behind the Scenes

Writing a..b is the same as calling a.rangeTo(b).

The operator form is just nicer syntax for the same function.

fun main() {
    val r1 = 1..5
    val r2 = 1.rangeTo(5)
    println(r1.toList() == r2.toList())
}

Choosing the Right Range

Quick guidance:

  • Numbers and chars → iterate and check
  • Doubles and custom comparables → check membership only
  • Need a step? stick to integer or char progressions
fun main() {
    println('m' in 'a'..'z')
    println(3.5 in 1.0..4.0)
    println(50 in 1..100)
}

Putting It Together

Ranges generalize across many types:

  • Char ranges iterate and validate
  • Long ranges handle large spans
  • Comparable and custom types support membership
  • .. is sugar for rangeTo
fun main() {
    val vowels = "aeiou"
    for (c in 'a'..'z') {
        if (c in vowels) print(c)
    }
    println()
}

Quick Check

Test your understanding of char and custom ranges.

Recap

You learned about char and custom ranges:

  • Char and Long ranges iterate like Int
  • Comparable types support membership via ..
  • Non-discrete types check in but cannot loop
  • .. is shorthand for rangeTo

That completes the Ranges and Progressions course.

Frequently asked questions

Is the “Char and Custom Ranges” lesson free?

Yes — the full text of “Char and Custom Ranges” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Char and Custom Ranges”?

Beyond integers. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Char and Custom Ranges” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Range Operators
  2. Step and downTo
  3. Ranges in Loops
  4. Char and Custom Ranges
← Back to Kotlin Academy