📚 Table of Contents
- 1. Introduction to Kotlin
- 2. Setting Up the Environment
- 3. Basic Syntax
- 4. Variables and Data Types
- 5. Functions
- 6. Conditionals and Loops
- 7. Lists, Sets, and Maps
- 8. Classes and Objects
- 9. Null Safety
- 10. Lambdas & Higher-Order Functions
- 11. Coroutines (Async Programming)
- 12. Best Practices
- 13. Mini Project
- 14. Conclusion
1. Introduction to Kotlin
Kotlin is a modern, concise, and powerful programming language developed by JetBrains. It runs on the JVM and is fully interoperable with Java. It’s widely used for Android development, backend, and even multiplatform apps.
- Official language for Android development.
- Interoperable with Java.
- Null-safe and concise syntax.
2. Setting Up the Environment
You can run Kotlin code in multiple ways:
- Using Kotlin Playground (online).
- Installing IntelliJ IDEA (official IDE).
- Using the Kotlin CLI compiler.
fun main() {
println("Hello, Kotlin!")
}
3. Basic Syntax
Every Kotlin program starts with a main function:
fun main() {
println("Welcome to Kotlin!")
}
println() for printing text.4. Variables and Data Types
Kotlin uses val for constants and var for mutable variables.
val name = "Alice" // Immutable
var age = 25 // Mutable
val pi: Double = 3.14 // Explicit type
Common data types include Int, Double, Boolean, String, and Char.
5. Functions
fun greet(name: String): String {
return "Hello, $name!"
}
fun main() {
println(greet("Bob"))
}
fun add(a: Int, b: Int) = a + b
6. Conditionals and Loops
val score = 85
if (score >= 90) {
println("Excellent")
} else if (score >= 70) {
println("Good")
} else {
println("Needs Improvement")
}
for (i in 1..5) {
println(i)
}
7. Lists, Sets, and Maps
val fruits = listOf("Apple", "Banana", "Cherry")
val numbers = mutableListOf(1, 2, 3)
val user = mapOf("name" to "John", "age" to 30)
println(fruits[0]) // Apple
println(user["name"]) // John
8. Classes and Objects
class Person(val name: String, var age: Int) {
fun introduce() {
println("Hi, I'm $name and I'm $age years old.")
}
}
fun main() {
val person = Person("Alice", 25)
person.introduce()
}
9. Null Safety
Kotlin eliminates null pointer exceptions using nullable types:
var name: String? = null
println(name?.length) // Safe call
println(name ?: "Unknown") // Elvis operator
10. Lambdas & Higher-Order Functions
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println(doubled)
11. Coroutines (Async Programming)
Coroutines make asynchronous code simple and readable:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(1000L)
println("World!")
}
println("Hello,")
}
kotlinx.coroutines.12. Best Practices
- Prefer
valovervarfor immutability. - Use meaningful function and variable names.
- Take advantage of null safety.
- Use Kotlin extensions and data classes.
13. Mini Project: Simple Calculator
fun add(a: Int, b: Int) = a + b
fun subtract(a: Int, b: Int) = a - b
fun multiply(a: Int, b: Int) = a * b
fun divide(a: Int, b: Int) = a / b
fun main() {
println("Sum: ${add(10, 5)}")
println("Product: ${multiply(4, 3)}")
}
readLine().14. Conclusion
Kotlin combines simplicity, power, and safety. It’s a great language for Android, backend, and cross-platform development.
Once you master Kotlin basics, explore advanced features like Coroutines, Generics, and Jetpack Compose.
Comments
Post a Comment