- Use
Bytewhen: working with raw binary data, network packets, file I/O, or memory-constrained environments (e.g., embedded). - Avoid
Bytewhen: doing general arithmetic, counters, indices — useIntinstead (faster and safer). Array<T>— generic, boxes primitives- Primitive arrays (
IntArray,DoubleArray, etc.) — no boxing, better performance Any— root type, likeObjectin JavaUnit— equivalent ofvoid(only one value:Unit)Nothing— type that has no values; used for functions that never return (e.g. throw exception)
In Kotlin, every value has a type, and all types are objects (you can call methods on numbers, characters, etc.). Here are the most important built-in types:
Numbers
Integer types: Byte (8-bit), Short (16-bit), Int (32-bit), Long (64-bit).
Floating-point types: Float (32-bit), Double (64-bit).
Use f or F suffix for Float literals: 3.14f.
fun main() {
val byteNum: Byte = 127
val shortNum: Short = 32000
val intNum = 2_000_000 // type inference → Int
val longNum = 9_000_000_000L // L suffix → Long
val doubleNum = 3.14159 // Double by default
val floatNum = 2.718f // f suffix → Float
println("Long: $longNum, Float: $floatNum")
}
When to Use Byte (and When Not To)
Boolean
Can only be true or false.
fun main() {
val isKotlinFun = true
val hasError = false
println("Is Kotlin fun? $isKotlinFun")
}
Char
Represents a single 16-bit Unicode character. Written in single quotes: 'A', 'μ', '\n'.
fun main() {
val letter: Char = 'K'
val digit = '7'
val newline = '\n'
println("First letter: $letter")
}
String
Immutable sequence of characters. Supports string templates with $var and ${expression}.
fun main() {
val name = "Kotlin"
val site = "www.zyasin.com"
val message = "Welcome to $site — learn $name today!"
println(message)
println("First char: ${message[0]}") // Safe indexing
}
Arrays
Fixed-size, mutable collections. Two kinds:
Important: == on arrays checks reference equality. Use contentEquals() or contentDeepEquals() to compare contents.
fun main() {
val numbers = intArrayOf(10, 20, 30) // Primitive array
val names = arrayOf("Ali", "Zara", "Sam")
names[1] = "Zain"
println(names.contentToString()) // [Ali, Zain, Sam]
println("Size: ${numbers.size}")
}
Nullable Types
Add ? to make any type nullable:
fun main() {
var email: String? = null
email = "hello@zyasin.com"
println(email?.length) // Safe call
println(email!!.length) // Force non-null (throws if null)
println(email ?: "No email") // Elvis operator
}
Collections
Immutable by default. Use Mutable... for changeable ones.
fun main() {
val fruits = listOf("apple", "banana") // Immutable
val scores = mutableMapOf(1 to "Gold", 2 to "Silver")
scores[3] = "Bronze"
println(scores)
}
Pair and Triple
Simple containers for 2 or 3 values (often used as map entries).
fun main() {
val point = Pair(10, 20)
val person = Triple("Ali", 25, true)
println("${person.first} is ${person.second} years old")
}
Special Types
fun main() {
val anything: Any = "Can be anything"
println(anything)
}
fun crash(): Nothing = throw RuntimeException("Boom!")
Quick Quiz
- Is there a function called
stringArrayOf()in Kotlin? - What happens if you access
numbers[10]in an array of size 5? - What is the difference between
ListandMutableList? - What does
Nothingrepresent?