KOTLIN Coding
Improving Versatile Arrangements & Calculations with KOTLIN
At Codelabpro, we make coding easier and enable learners with top-notch programming tutorials, courses, and resources. Whether you are a novice making your initial strides in coding or a seasoned developer aiming to improve your skills, we offer practical, real-world insights to assist you in achieving success.
Kotlin
Kotlin is a contemporary, open-source, statically typed programming language created by JetBrains. It is intended to interoperate with Java fully and is mainly utilized for creating Android applications, backend systems, and multiplatform projects. Kotlin is succinct, expressive, and safe, making it a favored option among developers.
Key Features:
- Interoperability with Java: Kotlin is capable of using Java libraries and frameworks seamlessly.
- Null Safety: Prevents null pointer exceptions (NPE) by design.
- Concise Syntax: Minimizes boilerplate code.
- Functional Programming: Facilitates functional programming paradigms.
- Multiplatform Development: Develop applications that function on Android, iOS, Web, and Desktop.
Hello Zameer in Kotlin
Explanation:
fun: Keyword to define a function.
main: Entry point of a Kotlin program.
println: Prints text to the console.
fun main() {
println(“Hello, Zameer!”)
}
1. Variables
Kotlin uses two keywords to declare variables:
- val: Immutable variable (read-only).
- var: Mutable variable (can be reassigned).
- val ensures name cannot be changed.
- var allows age to be reassigned.
fun main() {
val name: String = “Malik” // Immutable
var age: Int = 11 // Mutable
println(“Name: $name, Age: $age”)
}
2. Functions
- fun: Declares a function.
- Parameters (a, b)
- return type (Int) are explicitly declared.
fun add(a: Int, b: Int): Int {
return a + b
}
fun main() {
val result = add(5, 3)
println(“Sum: $result”)
}
3. Conditions and If Statements
fun main() {
val number = 5
if (number % 2 == 0) {
println(“$number is even”)
} else {
println(“$number is odd”)
}}
- Use if to indicate a section of code that should run, if a given condition is true
- Use else to indicate a section of code that should run, if the same condition is false
- Use else if to indicate a new condition to evaluate, if the initial condition is false
- Use switch to indicate multiple alternative sections of code to be executed
int x = 20;
int y = 18;
if (x > y) {
printf(“x is greater than y”);
}
4. Null Safety
Kotlin ensures null safety at compile time to avoid NullPointerException
.
Explanation:
- Adding
?
after the type allows it to storenull
. - The safe call operator (
?.
) prevents crashes if the variable is null.
fun main() {
var name: String? = null // Nullable type
println(name?.length) // Safe call operator (?)
}
5. Classes and Objects
class Person(val name: String, var age: Int) {
fun introduce() {
println(“Hi, my name is $name, and I am $age years old.”)
}}
fun main() {
val person = Person(“Alice”, 25)
person.introduce()
}
6. Collections
Kotlin supports both mutable and immutable collections.
Explanation
listOf: Creates a read-only list.
mutableListOf: Allows modifications.
- Lists – Ordered groups of items that permit duplicates.
- Sets – Unordered groups of distinct items.
- Maps – Groups of key-value pairs, where each key is distinct.
- Arrays – Collections of a fixed size with a defined type.
- Sequences – Collections of items that are evaluated lazily and can be handled in a pipeline.
fun main() {
val numbers = listOf(1, 2, 3) // Immutable list
val mutableNumbers = mutableListOf(4, 5, 6)
// Mutable list
mutableNumbers.add(7)
println(numbers)
println(mutableNumbers)
}
7. Lambda Functions
val square
: Declares a lambda function.{ number -> number * number }
: Lambda syntax.
fun main() {
val square: (Int) -> Int = { number -> number * number }
println(square(4))
}
8. Extension Functions
- Extension functions enhance existing classes without modifying their source code.
fun String.addExclamation(): String {
return this + “!”
}
fun main() {
println(“Hello”.addExclamation())
}
9. Coroutines (Asynchronous Programming)
- launch: Starts a coroutine.
- delay: Suspends the coroutine for a given time.
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(1000L)
println(“World!”)
}
println(“Hello,”)
}