codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

36 / 68 topics
36Kotlin Standard Library37Collections Functions38Sequences in Kotlin39Date and Time API40Kotlin Serialization41Kotlin Coroutines Library42Kotlin HTML DSL43Kotlin CLI
Tutorials/Kotlin/Kotlin Standard Library
🎯Kotlin

Kotlin Standard Library

Updated 2026-04-20
3 min read

Kotlin Standard Library

The Kotlin Standard Library is a comprehensive set of tools and utilities that come bundled with the Kotlin programming language. It provides essential functionalities for common tasks, making it easier for developers to write clean, efficient, and idiomatic Kotlin code. This guide will cover key components of the Kotlin Standard Library, including collections, strings, numbers, and more.

Introduction

Kotlin's standard library is designed to be lightweight yet powerful, offering a wide range of features that cater to various programming needs. It is built on top of Java's standard library and extends its capabilities while maintaining compatibility with existing Java codebases. Understanding the Kotlin Standard Library is crucial for any developer looking to harness the full potential of Kotlin.

Collections

Collections are fundamental data structures in any programming language, and Kotlin provides a rich set of collection types and functions to manipulate them efficiently.

List

A list in Kotlin is an ordered collection that can contain duplicate elements. Lists are immutable by default but mutable versions are also available.

// Immutable list
val immutableList = listOf("Apple", "Banana", "Cherry")

// Mutable list
val mutableListOf = mutableListOf("Apple", "Banana")
mutableListOf.add("Cherry")

Set

A set is a collection that does not allow duplicate elements. Like lists, sets can be mutable or immutable.

// Immutable set
val immutableSet = setOf(1, 2, 3)

// Mutable set
val mutableSet = mutableSetOf(1, 2)
mutableSet.add(3)

Map

A map is a collection that associates keys with values. Maps can also be mutable or immutable.

// Immutable map
val immutableMap = mapOf("one" to 1, "two" to 2)

// Mutable map
val mutableMap = mutableMapOf("one" to 1)
mutableMap["two"] = 2

Collection Functions

Kotlin provides a plethora of extension functions for collections, making common operations concise and expressive.

val numbers = listOf(1, 2, 3, 4, 5)

// Filtering
val evenNumbers = numbers.filter { it % 2 == 0 }

// Mapping
val doubledNumbers = numbers.map { it * 2 }

// Reduction
val sumOfNumbers = numbers.sum()

Strings

Strings in Kotlin are immutable sequences of characters. The standard library provides numerous functions to manipulate and query strings.

Basic Operations

val greeting = "Hello, Kotlin!"

// Length
println(greeting.length) // Output: 13

// Concatenation
val farewell = "Goodbye, Kotlin!"
val message = "$greeting $farewell"

// Substring
val substring = greeting.substring(0, 5) // Output: Hello

String Templates

Kotlin supports string templates, allowing for easy inclusion of variables and expressions within strings.

val name = "Alice"
val age = 30
println("Name: $name, Age: $age") // Output: Name: Alice, Age: 30

// Expression in template
println("Next year, you'll be ${age + 1} years old.") // Output: Next year, you'll be 31 years old.

String Functions

val text = "Hello, Kotlin!"

// Checking for presence
println(text.contains("Kotlin")) // Output: true

// Splitting
val parts = text.split(", ")
println(parts) // Output: [Hello, Kotlin!]

// Trimming
val paddedText = "   Hello, Kotlin!   "
println(paddedText.trim()) // Output: Hello, Kotlin!

Numbers

Kotlin supports various numeric types, including integers, floating-point numbers, and more. The standard library provides functions for common arithmetic operations and conversions.

Basic Operations

val a = 10
val b = 3

// Addition
println(a + b) // Output: 13

// Subtraction
println(a - b) // Output: 7

// Multiplication
println(a * b) // Output: 30

// Division
println(a / b) // Output: 3 (integer division)

Conversions

val intNumber = 123
val doubleNumber = intNumber.toDouble() // Convert to Double

val floatNumber = 45.67f
val intFromFloat = floatNumber.toInt() // Convert to Int

Ranges

Ranges are a convenient way to represent sequences of numbers or characters in Kotlin.

// Numeric range
val numberRange = 1..10
for (i in numberRange) {
    println(i)
}

// Character range
val charRange = 'a'..'z'
for (char in charRange) {
    print(char)
}

Exceptions

Kotlin uses exceptions for error handling, similar to Java. The standard library provides a set of exception classes and functions to manage errors gracefully.

Throwing Exceptions

fun divide(a: Int, b: Int): Int {
    if (b == 0) throw ArithmeticException("Division by zero")
    return a / b
}

Catching Exceptions

try {
    val result = divide(10, 0)
} catch (e: ArithmeticException) {
    println("Error: ${e.message}")
} finally {
    println("Execution completed.")
}

Best Practices

  1. Use Immutable Collections by Default: Prefer immutable collections unless mutability is explicitly required.
  2. Leverage Collection Functions: Utilize Kotlin's powerful collection functions to write concise and readable code.
  3. String Templates for Clarity: Use string templates to make your code more readable and maintainable.
  4. Handle Exceptions Gracefully: Always handle exceptions using try-catch blocks to prevent unexpected crashes.

Conclusion

The Kotlin Standard Library is a robust foundation that simplifies many common programming tasks. By mastering its features, you can write more efficient, idiomatic, and maintainable Kotlin code. Whether you're working with collections, strings, numbers, or exceptions, the standard library provides the tools you need to succeed in Kotlin development.


This tutorial provides a comprehensive overview of the Kotlin Standard Library, covering essential components and best practices. By following this guide, you'll be well-equipped to leverage the full power of Kotlin's standard library in your projects.


PreviousRepeatable AnnotationsNext Collections Functions

Recommended Gear

Repeatable AnnotationsCollections Functions