Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 शुरुआत  ·  Lesson 05

Android के लिए Kotlin Basics

Kotlin क्यों?

Kotlin Android के लिए Google की पसंदीदा language है। Java के मुकाबले इसमें कम code लगता है, null safety से बहुत सारे crashes रुकते हैं, और यह हर Android library के साथ चलती है। नीचे का सब plain Kotlin में चलता है — दिखाए गए outputs असली हैं।

Java

  • String name = "Riya";
  • बहुत boilerplate (getters, setters)
  • NullPointerException आम

Kotlin

  • val name = "Riya"
  • एक line में data class
  • Null safety built in
वही JVM, बहुत छोटा code

Practice कहाँ करें

Browser में play.kotlinlang.org use करें, या Android Studio में fun main() वाली Practice.kt file बनाकर उसके बगल वाले हरे ▶ पर click करें।

Variables: val और var

Kotlin
fun main() {
    val appName = "Student Diary"      // val = cannot change
    var notes = 3                      // var = can change
    notes += 2
    val price: Double = 49.5
    val isPro = false
    println("$appName has $notes notes")
    println("Price: ₹$price, pro = $isPro")
    println("Next year: ${2026 + 1}")
}
Student Diary has 5 notes Price: ₹49.5, pro = false Next year: 2027

val

  • read-only (final जैसा)
  • val appName = "Diary"
  • appName = "X" → error

var

  • दोबारा assign हो सकता है
  • var notes = 3
  • notes += 2 → 5
val को प्राथमिकता दें

Basic types

TypeExampleUse
Int / Long42 / 1700000000000LCounts / millis में time
Double / Float49.5 / 4.5fPrices / view sizes
Booleantrue, falseisDone, isFavorite
String / Char"Maths" / 'A'Text

Kotlin आमतौर पर type infer कर लेता है: val n = 5 Int है। Convert करें: "42".toInt(), 5.toString(), 3.7.toInt()।

if और when

Kotlin
fun grade(marks: Int): String = when {
    marks >= 90 -> "A+"
    marks >= 75 -> "A"
    marks >= 60 -> "B"
    marks >= 33 -> "Pass"
    else -> "Fail"
}

fun main() {
    val marks = 78
    if (marks >= 33) println("Passed") else println("Failed")
    println("Grade: ${grade(marks)}")

    val day = 3
    val name = when (day) {
        1 -> "Monday"
        2 -> "Tuesday"
        3 -> "Wednesday"
        else -> "Other day"
    }
    println(name)
}
Passed Grade: A Wednesday

Loops और ranges

Kotlin
fun main() {
    for (i in 1..5) print("$i ")
    println()
    for (i in 10 downTo 0 step 5) print("$i ")
    println()
    val subjects = listOf("Maths", "Physics", "English")
    for ((index, s) in subjects.withIndex()) println("${index + 1}. $s")
    var tries = 0
    while (tries < 3) { tries++ }
    println("tries = $tries")
}
1 2 3 4 5 10 5 0 1. Maths 2. Physics 3. English tries = 3

Lists और maps

Kotlin
fun main() {
    val subjects = listOf("Maths", "Physics", "English")          // read-only
    val notes = mutableListOf("Homework")                           // can change
    notes.add("Revision")
    notes.remove("Homework")
    println(subjects.size)
    println(notes)

    val marks = mapOf("Maths" to 92, "Physics" to 81, "English" to 74)
    println(marks["Maths"])
    for ((subject, m) in marks) println("$subject → $m")

    val high = marks.filter { it.value >= 80 }.keys
    println(high)
    println(marks.values.average())
}
3 [Revision] 92 Maths → 92 Physics → 81 English → 74 [Maths, Physics] 82.33333333333333
marks map3 entriesfilter { value >= 80 }.keys[Maths, Physics]
Collection functions एक के बाद एक जुड़ते हैं

Functions

Kotlin
fun greet(name: String, city: String = "Aligarh"): String {
    return "Namaste $name from $city"
}

fun square(n: Int) = n * n          // single-expression function

fun percent(obtained: Int, total: Int = 100): Double = obtained * 100.0 / total

fun main() {
    println(greet("Riya"))
    println(greet("Aman", city = "Delhi"))   // named argument
    println(square(7))
    println("%.1f%%".format(percent(425, 500)))
}
Namaste Riya from Aligarh Namaste Aman from Delhi 49 85.0%

Android में यह कहाँ use होगा

Kotlin – app के अंदर
// The same Kotlin inside an Activity
val count = notes.size                                   // val
binding.tvTitle.text = "You have $count notes"           // string template
val color = when (note.priority) {                       // when
    2 -> R.color.priority_high
    0 -> R.color.priority_low
    else -> R.color.priority_medium
}
val pending = notes.filter { !it.isDone }                // collections + lambda

आम गलतियाँ

  • val को दोबारा assign करना → "Val cannot be reassigned"।
  • listOf() में add करना → mutableListOf() use करें।
  • "5" + 3 "53" (text) देता है — पहले convert करें: "5".toInt() + 3।
  • 1..5 में 5 शामिल है; 4 पर रुकने के लिए 1 until 5 use करें।

सारांश

  • val (fixed) और var (बदलने वाला); types infer होते हैं।
  • when switch की जगह लेता है और value लौटाता है।
  • for (i in 1..5), listOf, mapOf, filter, map।
  • Default और named arguments वाले functions।
✅ Practice

result(marks: List<Int>) function लिखिए जो total, percentage और when से grade print करे। listOf(78, 85, 91, 66, 72) से test कीजिए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

val कब use करें और var कब?
Default रूप से val use करें। var तभी use करें जब value को बाद में सच में बदलना हो। कम बदलने वाली values मतलब कम bugs।
क्या when, switch जैसा है?
यह switch की जगह लेता है पर ज़्यादा powerful है: ranges और conditions check कर सकता है, और value लौटाता है जिसे variable में रख सकते हैं।
listOf और mutableListOf में क्या फ़र्क है?
listOf read-only list बनाता है। mutableListOf ऐसी list बनाता है जिसमें add और remove कर सकते हैं।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicKotlin OOP, Null Safety और Lambdas आपकी प्रगति: 5 / 43 (12%) · Lesson 6 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें