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

Kotlin Standard Library क्या है

Kotlin standard library तैयार-बने functions का बड़ा toolbox है जो मुफ़्त में Kotlin के साथ ही bundled आता है।

String Utilities

Standard library कई सुविधाजनक string functions प्रदान करती है, जैसे किसी collection से एक string बनाने के लिए .joinToString(), और किसी string को दी गई संख्या में दोहराने के लिए .repeat()।

उदाहरण: 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

.coerceIn(min, max) जैसे functions किसी number को एक दी गई range तक clamp करते हैं, और .coerceAtLeast()/.coerceAtMost() इसे सिर्फ एक bound तक clamp करते हैं।

उदाहरण: 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

Standard library के collection functions -- .map, .filter, .sorted, .groupBy, और बहुत कुछ -- manual loop के बिना रोज़ाना की data transformation ज़रूरतों के बड़े हिस्से को cover करते हैं।

उदाहरण: 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), और date/time handling (kotlinx.datetime) सभी JetBrains द्वारा maintained अलग kotlinx libraries हैं, जिन्हें अपनी dependency चाहिए, भले ही वे language के स्वाभाविक विस्तार जैसी महसूस हों।

उदाहरण: 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. #}
आम गलतियां
  1. दो numbers में max ढूंढने या किसी list को text में जोड़ने जैसी आम utilities को फिर से implement करना, जिन्हें standard library पहले से प्रदान करती है।
  2. यह मान लेना कि हर सुविधाजनक function (जैसे coroutines या serialization) standard library का हिस्सा है, जबकि कई अलग kotlinx libraries हैं जिन्हें अपनी dependency चाहिए।
  3. जिन tasks को वे पहले से संक्षेप में हल करते हैं उनके लिए manual loops लिखने से पहले collection, string, और scope function utilities को explore न करना।
चैप्टर सारांश
  • Kotlin standard library (kotlin-stdlib) compiler के साथ ship होती है और collections, string utilities, scope functions, और बहुत कुछ प्रदान करती है, बिना किसी अतिरिक्त dependency के।
  • kotlinx.coroutines, kotlinx.serialization, और kotlinx.datetime जैसी libraries standard library से अलग हैं और explicitly जोड़नी चाहिए।
  • आम standard library helpers में .joinToString(), .coerceIn(), .repeat(), और पहले कवर किए गए scope functions शामिल हैं।
  • पहले standard library जांचना अक्सर आम tasks के लिए verbose manual logic लिखने से बचाता है।
🔒

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.