Kotlin Data Types

Kotlin Data Types

Data types in Kotlin define what kind of data a variable can hold. Kotlin is a statically typed language, which means every variable has a specific data type.


1. Numbers (Numeric Data Types)

Integer Types

Type Size
Byte 8-bit
Short 16-bit
Int 32-bit
Long 64-bit
val a: Int = 100
val b: Long = 100000000L
val c: Byte = 10
val d: Short = 200

Floating-Point Types

Type Size
Float 32-bit
Double 64-bit
val pi: Double = 3.14159
val rate: Float = 9.5F

2. Boolean Data Type

Used for true or false values.

val isActive: Boolean = true
val isLoggedIn = false

3. Character (Char)

Stores a single character.

val grade: Char = 'A'
val symbol: Char = '@'

⚠ Use single quotes for Char.


4. String Data Type

Stores a sequence of characters.

val name: String = "Kotlin"
val message = "Welcome to Kotlin"

Multi-Line String

val text = """
Kotlin is
easy to learn
and powerful
"""


5. Arrays

Used to store multiple values of the same type.

val numbers = arrayOf(1, 2, 3, 4)
val names = arrayOf("A", "B", "C")

Access elements:

println(numbers[0]) // 1

6. Nullable Data Types

By default, variables cannot be null.

var city: String = "Delhi"
// city = null ❌

To allow null:

var city: String? = null

7. Type Conversion

Kotlin does not allow implicit conversion.

val x: Int = 10
val y: Double = x.toDouble()

Common conversions:

  • toInt()

  • toDouble()

  • toFloat()

  • toString()


8. Any, Unit, and Nothing

Any

Parent of all classes.

val value: Any = "Hello"

Unit

Represents no meaningful value (like void in Java).

fun show(): Unit {
println("Hello")
}

Nothing

Represents no value at all (used in exceptions).

fun fail(): Nothing {
throw Exception("Error")
}

Summary

  • Kotlin has strongly typed data types

  • Supports nullable and non-nullable types

  • No implicit type casting

  • Rich standard data types

You may also like...