Complete Kotlin Programming Tutorial
# Basics of Kotlin Programming Language
## Introduction to Kotlin
Kotlin is a modern, statically typed programming language that runs on the JVM and is officially
supported for Android development.
### Features:
- Concise & expressive syntax
- Null safety
- Interoperability with Java
- Coroutines for asynchronous programming
- Extension functions
## Hello World Program
```kotlin
fun main() {
println("Hello, Kotlin!")
}
```
## Variables and Data Types
```kotlin
val name: String = "Kotlin"
var age: Int = 25
val city = "Delhi"
var isActive = true
```
---
# Conditional Statements (Control Flow) in Kotlin
## If-Else Statement
```kotlin
val num = 10
if (num > 0) println("Positive Number") else println("Negative Number")
```
## When (Switch Case Alternative)
```kotlin
val day = 3
val dayName = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
else -> "Invalid day"
}
println(dayName)
```
---
# Functional Programming in Kotlin
## Functions
```kotlin
fun greet(name: String) {
println("Hello, $name!")
}
greet("Kotlin")
```
## Higher-Order Functions
```kotlin
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
val sum = operateOnNumbers(5, 3) { x, y -> x + y }
println(sum)
```
---
# Collections in Kotlin
## List
```kotlin
val numbers = listOf(1, 2, 3, 4, 5)
```
## Map and Filter
```kotlin
val nums = listOf(1, 2, 3, 4, 5)
val squared = nums.map { it * it }
println(squared)
```
---
# Object-Oriented Programming (OOP) in Kotlin
## Class and Object
```kotlin
class Car(val brand: String, val model: String) {
fun drive() {
println("$brand $model is driving")
}
}
val myCar = Car("Toyota", "Camry")
myCar.drive()
```
## Inheritance
```kotlin
open class Animal {
open fun sound() {
println("Animal makes a sound")
}
}
class Dog : Animal() {
override fun sound() {
println("Dog barks")
}
}
val myDog = Dog()
myDog.sound()
```
---
# Kotlin Exception Handling
## Try-Catch Example
```kotlin
try {
val result = 10 / 0
} catch (e: ArithmeticException) {
println("Cannot divide by zero!")
}
```
---
# Kotlin Null Safety
```kotlin
var name: String? = null
println(name?.length ?: "Name is null")
```
---
# Kotlin Scope Functions
## let, run, apply, also, with
```kotlin
data class User(var name: String, var age: Int)
val user = User("Alice", 25).apply {
name = "Bob"
age = 30
}
println(user)
```
---
# Kotlin Interoperability with Java
## Calling Java code from Kotlin
```kotlin
val javaObject = JavaClass()
javaObject.javaMethod()
```
---
# Kotlin Coroutines
## Asynchronous programming
```kotlin
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch {
delay(1000L)
println("World!")
}
println("Hello,")
Thread.sleep(2000L)
}
```
---
# Miscellaneous
## Extension Functions
```kotlin
fun String.greet() = "Hello, $this"
println("Kotlin".greet())
```
## Data Classes
```kotlin
data class Person(val name: String, val age: Int)
val person = Person("Alice", 22)
println(person)
```
# Conclusion
Kotlin is a powerful and versatile language suitable for Android development, backend applications,
and more.