Kotlin परिचय
What is Kotlin?
Kotlin एक general-purpose, statically-typed programming language है जिसे JetBrains ने Java के एक ज़्यादा संक्षिप्त और सुरक्षित विकल्प के रूप में बनाया।
यह Java Virtual Machine (JVM) पर चलती है, इसलिए यह किसी भी मौजूदा Java library का उपयोग कर सकती है, लेकिन यह null safety, data classes, और extension functions जैसी modern features जोड़ती है।
kotlinc compiler किसी भी terminal से plain .kt files चला सकता है।उदाहरण: What is Kotlin?
// Entry point: execution of the program starts here
fun main() {
// Print "Kotlin is a modern programming language" to the console, with a trailing newline
println("Kotlin is a modern programming language")
// Print "It runs on the JVM and beyond" to the console, with a trailing newline
println("It runs on the JVM and beyond")
}
Login to try C/C++/Java/PHP code in the editor
Why Kotlin Was Created
JetBrains ने रोज़ Java लिखते समय उन्हें झेलनी पड़ी दिक्कतों को ठीक करने के लिए Kotlin बनाई, जैसे verbose boilerplate और null pointer exceptions का जोखिम।
Kotlin Java के साथ पूरी interoperability बनाए रखती है जबकि सुरक्षित defaults और कहीं ज़्यादा संक्षिप्त syntax जोड़ती है।
उदाहरण: Why Kotlin Was Created
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `goals`, set to `listOf("conciseness", "safety", "interoperability")`
val goals = listOf("conciseness", "safety", "interoperability")
// Loop over `goals`, binding each item to `goal`
for (goal in goals) {
// Print "Design goal: $goal" to the console, with a trailing newline
println("Design goal: $goal")
}
}
Login to try C/C++/Java/PHP code in the editor
Where Kotlin Is Used
Android app development के लिए Kotlin पसंदीदा language है, लेकिन इसका उपयोग server-side backends (Ktor और Spring जैसे frameworks के साथ), iOS और Android के बीच share किए multiplatform mobile code के लिए, और general scripting के लिए भी होता है।
उदाहरण: Where Kotlin Is Used
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `usedFor`, set to `listOf("Android apps" to "Google apps", "Backend" to "Ktor servers", "Scripting" to "build scripts")`
val usedFor = listOf("Android apps" to "Google apps", "Backend" to "Ktor servers", "Scripting" to "build scripts")
for ((area, example) in usedFor) {
// Print "$area: $example" to the console, with a trailing newline
println("$area: $example")
}
}
Login to try C/C++/Java/PHP code in the editor
Compiled on the JVM
Kotlin source code चलने से पहले JVM bytecode में compile होता है, बिल्कुल Java की तरह।
यह compiler को program चलने से पहले ही type errors जैसी कई गलतियां पकड़ने देता है, और यह Kotlin को बिना किसी अतिरिक्त काम के Java code को call करने देता है और उल्टा भी।
kotlinc file.kt -include-runtime -d app.jar चलाना फिर java -jar app.jar दो तेज़ steps में compile और run करता है।उदाहरण: Compiled on the JVM
// Entry point: execution of the program starts here
fun main() {
// Print "This code is compiled to JVM bytecode" to the console, with a trailing newline
println("This code is compiled to JVM bytecode")
// Print "The compiler already checked it for errors" to the console, with a trailing newline
println("The compiler already checked it for errors")
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Kotlin सिर्फ Android apps के लिए है और इसे backend, scripting, या साधारण command-line programs के लिए उपयोग नहीं किया जा सकता।
- Kotlin को Java के साथ confuse करना; Kotlin एक अलग, नई language है जिसका अपना syntax है भले ही यह एक ही JVM पर चलती हो।
- यह सोचना कि Kotlin code को हमेशा Android Studio चाहिए चलने के लिए, जबकि एक साधारण
.ktfile को terminal से compile और run किया जा सकता है।
- Kotlin एक modern, statically-typed language है जिसे JetBrains ने बनाया और 2011 में release किया।
- यह JVM पर चलती है, JavaScript में compile होती है, और Kotlin/Native के ज़रिए natively compile होती है।
- Google ने 2017 में Kotlin को Android development के लिए officially supported language बनाया।
- Kotlin पूरी तरह Java के साथ interoperate करती है, इसलिए मौजूदा Java libraries को सीधे Kotlin code से उपयोग किया जा सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: