← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 2 of 7

The Kotlin Standard Library

The Kotlin standard library is the big toolbox of ready-made functions that comes bundled with Kotlin itself, for free.

String Utilities

The standard library provides many convenient string functions, such as .joinToString() to build a string from a collection, and .repeat() to repeat a string a given number of times.

Example: String Utilities

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
    val fruits = listOf("apple", "banana", "cherry")
    // Print `fruits.joinToString(", ")` to the console, with a trailing newline
    println(fruits.joinToString(", "))
    // Print `"ab".repeat(3)` to the console, with a trailing newline
    println("ab".repeat(3))
}

Numeric Utilities

Functions like .coerceIn(min, max) clamp a number to a given range, and .coerceAtLeast()/.coerceAtMost() clamp it to just one bound.

Example: Numeric Utilities

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `value`, set to `150`
    val value = 150
    // Print "Clamped: ${value.coerceIn(0, 100)}" to the console, with a trailing newline
    println("Clamped: ${value.coerceIn(0, 100)}")
    // Print "At most 50: ${value.coerceAtMost(50)}" to the console, with a trailing newline
    println("At most 50: ${value.coerceAtMost(50)}")
}

Collection Utilities Recap

The standard library's collection functions -- .map, .filter, .sorted, .groupBy, and many more -- cover the vast majority of everyday data transformation needs without a manual loop.

Example: Collection Utilities Recap

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(5, 3, 8, 1, 9)`
    val numbers = listOf(5, 3, 8, 1, 9)
    // Print "Sorted: ${numbers.sorted()}" to the console, with a trailing newline
    println("Sorted: ${numbers.sorted()}")
    // Print "Max: ${numbers.max()}" to the console, with a trailing newline
    println("Max: ${numbers.max()}")
    // Print "Chunked: ${numbers.chunked(2)}" to the console, with a trailing newline
    println("Chunked: ${numbers.chunked(2)}")
}

What's Not in the Standard Library

Coroutines (kotlinx.coroutines), serialization (kotlinx.serialization), and date/time handling (kotlinx.datetime) are all separate kotlinx libraries maintained by JetBrains, requiring their own dependency, even though they feel like natural extensions of the language.

Example: What's Not in the Standard Library

markup
fun main() {
    // kotlin-stdlib alone is enough for everything in this example
    val date = "2024-01-15"
    val parts = date.split("-")
    println("Year: ${parts[0]}, Month: ${parts[1]}, Day: ${parts[2]}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reimplementing common utilities, like finding the max of two numbers or joining a list into text, that the standard library already provides.
  2. Assuming every convenient function (like coroutines or serialization) is part of the standard library, when many are separate kotlinx libraries requiring their own dependency.
  3. Not exploring collection, string, and scope function utilities before writing manual loops for tasks they already solve concisely.
Chapter Summary
  • The Kotlin standard library (kotlin-stdlib) ships with the compiler and provides collections, string utilities, scope functions, and more, with no extra dependency needed.
  • Libraries like kotlinx.coroutines, kotlinx.serialization, and kotlinx.datetime are separate from the standard library and must be added explicitly.
  • Common standard library helpers include .joinToString(), .coerceIn(), .repeat(), and the scope functions covered earlier.
  • Checking the standard library first often avoids writing verbose manual logic for common tasks.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.