Definition: Data types specify the type of data that a variable can hold. Kotlin is a statically-typed language, meaning you need to declare the type of variable if it cannot be inferred.
Explanation: Kotlin provides several data types including primitive types and more complex types.
Primitive Data Types:
Byte
: 8-bit integerShort
: 16-bit integerInt
: 32-bit integerLong
: 64-bit integerFloat
: 32-bit floating-pointDouble
: 64-bit floating-pointChar
: 16-bit Unicode characterBoolean
: true or falseExample:
fun main() {
val num: Int = 10
val price: Double = 19.99
val letter: Char = 'A'
val isKotlinFun: Boolean = true
println("Integer: $num")
println("Double: $price")
println("Character: $letter")
println("Boolean: $isKotlinFun")
}
Definition: Variables are containers for storing data values. In Kotlin, you declare variables with val
(read-only) or var
(mutable).
Explanation: Variables can hold primitive types or objects. The val
keyword denotes an immutable reference, while var
allows reassignment.
Example:
fun main() {
var mutableVariable: Int = 10
val immutableVariable: Int = 20
println("Mutable Variable: $mutableVariable")
println("Immutable Variable: $immutableVariable")
mutableVariable = 30
println("Updated Mutable Variable: $mutableVariable")
}
Definition: Loops are used to execute a block of code repeatedly based on a condition.
Explanation: Kotlin supports for
, while
, and do-while
loops.
for (i in 1..5) {
println("Iteration: $i")
}
val numbers = arrayOf(1, 2, 3, 4, 5)
for (number in numbers) {
println("Number: $number")
}
var i = 0
while (i < 5) {
println("Iteration: $i")
i++
}
var i = 0
do {
println("Iteration: $i")
i++
} while (i < 5)
Definition: Conditions control the flow of the program based on certain criteria.
Explanation: Kotlin supports conditional statements like if
, when
, and ternary-like if-else
expressions.
val number = 10
if (number > 0) {
println("Number is positive.")
} else {
println("Number is non-positive.")
}
val day = 3
when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid day")
}
Definition: Functions are blocks of code that perform a specific task and can be invoked multiple times.
Explanation: Kotlin functions are declared using the fun
keyword and can return values or be void.
Example:
fun add(a: Int, b: Int): Int {
return a + b
}
fun main() {
val sum = add(5, 3)
println("Sum: $sum")
}
Definition: Classes are blueprints for creating objects. They encapsulate data for the object and methods to manipulate that data.
Explanation: Kotlin classes are defined using the class
keyword. Kotlin also provides features like data classes and companion objects.
Example:
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("John", 30)
person.introduce()
}
Definition: Data classes are a special type of class in Kotlin that automatically provides useful methods such as toString
, equals
, and hashCode
.
Explanation: Use the data
keyword to declare a data class.
Example:
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 25)
val user2 = User("Alice", 25)
println(user1)
println(user1 == user2) // true
}
Definition: Companion objects are a way to define static-like members in a class.
Explanation: A companion object is an object declaration inside a class that can access its private members. It’s instantiated only once and can hold constants and functions.
Example:
class MathUtils {
companion object {
const val PI = 3.14159
fun square(x: Int): Int {
return x * x
}
}
}
fun main() {
println(MathUtils.PI)
println(MathUtils.square(4))
}
Definition: Scoping functions help to simplify code by defining the scope of an object.
Explanation: Kotlin includes scoping functions like let
, run
, apply
, with
, and also
.
Example:
val str = "Kotlin"
str.let {
println("String length: ${it.length}")
}
val result = StringBuilder().apply {
append("Hello, ")
append("World!")
}.toString()
println(result)