</>
Skip to content
Kotlin lessons (8/33)

Kotlin — Data Types

Basic types

TypeDescription
Booleantrue or false
Byte8-bit signed
Short16-bit signed
Int32-bit signed
Long64-bit signed
Float32-bit float
Double64-bit float
CharUnicode character
StringUTF-8 text

Number types

fun main() {
    val b: Byte = 127
    val s: Short = 32000
    val i: Int = 2147483647
    val l: Long = 9223372036854775807L
    val f: Float = 3.14f
    val d: Double = 3.141592653589793

    println("$b, $s, $i, $l, $f, $d")
}

Strings

val s = "Hello"
val raw = """Raw string
with newlines"""
val interpolated = "Length: ${s.length}"

println(interpolated) // Length: 5

Collections

// List (immutable)
val nums = listOf(1, 2, 3, 4, 5)

// MutableList
val mutable = mutableListOf(1, 2, 3)
mutable.add(4)

// Map
val map = mapOf("Alice" to 30, "Bob" to 25)

// Set
val set = setOf(1, 2, 3, 2, 1) // [1, 2, 3]

// Empty collections
val emptyList = emptyList<String>()
val emptyMap = emptyMap<String, Int>()

Data classes

data class Person(val name: String, val age: Int)

fun main() {
    val p1 = Person("Alice", 30)
    val p2 = Person("Alice", 30)

    println(p1)            // Person(name=Alice, age=30)
    println(p1 == p2)      // true (structural equality)
    println(p1.name)       // Alice

    // Copy
    val p3 = p1.copy(age = 31)
    println(p3)            // Person(name=Alice, age=31)
}

Type aliases

typealias UserId = String
typealias UserMap = Map<UserId, User>

fun main() {
    val id: UserId = "user-123"
    println(id)
}

Mini Practice

Write Kotlin code that:

  1. Creates variables of each primitive type
  2. Uses a data class with destructuring
  3. Creates and manipulates a MutableList
  4. Demonstrates a type alias

Up Next

In the next lesson, you'll learn about Operators — arithmetic and logical operations in Kotlin.

Related Topics

Frequently Asked Questions about Data Types

What is Data Types in Kotlin?

Data Types is a fundamental concept in Kotlin. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Data Types?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.

Why is Data Types important in Kotlin?

Data Types is essential for Kotlin development. Understanding this concept will help you write better code and solve real-world problems more effectively.