0% found this document useful (0 votes)
12 views

Kotlin

Android Programming

Uploaded by

MARK SIMIYU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Kotlin

Android Programming

Uploaded by

MARK SIMIYU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here’s a concise overview of Fundamentals of Kotlin, a modern, concise, and expressive

programming language often used for Android development:

1. Introduction to Kotlin

• Kotlin is a statically typed, cross-platform programming language developed by


JetBrains.
• Fully interoperable with Java.
• Known for its concise syntax and null safety.

2. Basic Syntax

• Kotlin code is cleaner and more concise compared to Java.

fun main() {
println("Hello, Kotlin!") // Prints output
}

3. Variables

• Kotlin has two types of variable declarations:


o val: Immutable (read-only, like a constant).
o var: Mutable (can be reassigned).

val name = "Isaac" // Immutable


var age = 25 // Mutable

4. Data Types

Kotlin has built-in types similar to Java, but with better null safety:

• Int, Double, Float, Boolean, Char, String

val isKotlinFun: Boolean = true


val grade: Char = 'A'
val pi: Double = 3.14
val name: String = "Isaac"
5. Control Flow

• If-Else is an expression that returns a value.


• Kotlin also supports when as an alternative to switch.

// If-Else
val age = 18
val status = if (age >= 18) "Adult" else "Minor"

// When Expression
val day = 2
val dayName = when (day) {
1 -> "Monday"
2 -> "Tuesday"
else -> "Unknown"
}

6. Loops

• Kotlin supports for, while, and do-while.

// For Loop
for (i in 1..5) {
println(i) // Prints 1 to 5
}

// While Loop
var count = 5
while (count > 0) {
println(count--)
}

7. Functions

• Functions in Kotlin are defined using the fun keyword.

fun greet(name: String): String {


return "Hello, $name!"
}

// Shorter version
fun add(a: Int, b: Int) = a + b

8. Classes and Objects

Kotlin supports object-oriented programming with classes, constructors, and inheritance.


class Person(val name: String, var age: Int) {
fun greet() = "Hi, I’m $name, and I’m $age years old."
}

fun main() {
val person = Person("Isaac", 28)
println(person.greet())
}

9. Collections

Kotlin provides immutable and mutable collections:

• List, Set, Map

val fruits = listOf("Apple", "Banana", "Cherry") // Immutable List


val mutableFruits = mutableListOf("Apple", "Banana")
mutableFruits.add("Cherry") // Mutable List

10. Null Safety

• Kotlin prevents null pointer exceptions with its null safety feature.
• Use ? to declare nullable variables and !! to forcefully access nullable variables.

var nullableName: String? = null


println(nullableName?.length) // Safe call (prints null)
nullableName = "Isaac"
println(nullableName!!.length) // Non-null assertion (prints 5)

11. Lambdas and Higher-Order Functions

• Kotlin supports functional programming with lambdas and higher-order functions.

val numbers = listOf(1, 2, 3, 4)


val doubled = numbers.map { it * 2 } // Lambda
println(doubled) // Output: [2, 4, 6, 8]

12. Extensions

• Kotlin allows extending existing classes with new functions.

fun String.greet() = "Hello, $this!"

fun main() {
println("Isaac".greet()) // Output: Hello, Isaac!
}

13. Coroutines

• Kotlin provides coroutines for asynchronous programming.

import kotlinx.coroutines.*

fun main() = runBlocking {


launch {
delay(1000L)
println("World!")
}
println("Hello,")
}

14. Interoperability with Java

• Kotlin can seamlessly call Java code, and vice versa.

// Calling a Java method from Kotlin


val list = ArrayList<String>()
list.add("Kotlin")
println(list[0])

Would you like to explore Kotlin for a specific use case like Android development or backend?

You might also like