Kotlin Data Types
Understanding different types of data in Kotlin
🎯 What are Kotlin Data Types?
Data types specify what kind of data a variable can hold. Kotlin has built-in types like numbers, text, and booleans to handle different information efficiently.
// Different data types
val age: Int = 25 // Whole number
val price: Double = 19.99 // Decimal number
val name: String = "Alice" // Text
val isActive: Boolean = true // True/False
Output:
age: 25 (Int)
price: 19.99 (Double)
name: Alice (String)
isActive: true (Boolean)
Basic Data Types
Numbers
Int, Long, Float, Double
val count: Int = 42
Text
String and Char
val text: String = "Hello"
Boolean
True or False values
val isReady: Boolean = true
Character
Single character
val grade: Char = 'A'
🔹 Number Data Types
Kotlin provides different number types for different ranges and precision:
// Integer types
val smallNumber: Byte = 127 // -128 to 127
val regularNumber: Short = 32000 // -32,768 to 32,767
val bigNumber: Int = 2000000 // -2^31 to 2^31-1
val hugeNumber: Long = 9000000000L // -2^63 to 2^63-1
// Decimal types
val price: Float = 19.99f // 32-bit floating point
val precise: Double = 3.14159265 // 64-bit floating point (default)
// Automatic type inference
val autoInt = 42 // Int
val autoDouble = 3.14 // Double
val autoLong = 1000000000000 // Long (if too big for Int)
Output:
smallNumber: 127 (Byte)
regularNumber: 32000 (Short)
bigNumber: 2000000 (Int)
hugeNumber: 9000000000 (Long)
price: 19.99 (Float)
precise: 3.14159265 (Double)
🔹 String and Character Types
Handle text data with String and Char types:
// String examples
val firstName = "John"
val lastName = "Doe"
val fullName = "$firstName $lastName" // String interpolation
val multiLine = """
This is a
multi-line
string
""".trimIndent()
// Character examples
val firstLetter: Char = 'J'
val grade: Char = 'A'
val symbol: Char = '@'
// String operations
val message = "Hello, World!"
val length = message.length // 13
val upperCase = message.uppercase() // "HELLO, WORLD!"
val contains = message.contains("World") // true
Output:
fullName: John Doe
firstLetter: J
length: 13
upperCase: HELLO, WORLD!
contains: true
🔹 Boolean Data Type
Boolean represents true/false values, essential for decision making:
// Boolean variables
val isLoggedIn = true
val hasPermission = false
val isAdult = true
// Boolean operations
val canAccess = isLoggedIn && hasPermission // AND operation
val needsHelp = !isAdult // NOT operation
val showContent = isLoggedIn || isAdult // OR operation
// Comparison results are Boolean
val age = 18
val isEligible = age >= 18 // true
val isChild = age < 13 // false
// Using in conditions
val status = if (isLoggedIn) "Welcome!" else "Please log in"
Output:
canAccess: false (true AND false)
needsHelp: false (NOT true)
showContent: true (true OR true)
isEligible: true
status: Welcome!
🔹 Type Conversion
Convert between different data types when needed:
// Explicit type conversion
val intValue = 42
val doubleValue = intValue.toDouble() // 42.0
val stringValue = intValue.toString() // "42"
// String to number conversion
val numberString = "123"
val convertedInt = numberString.toInt() // 123
val convertedDouble = numberString.toDouble() // 123.0
// Safe conversion (returns null if conversion fails)
val invalidString = "abc"
val safeInt = invalidString.toIntOrNull() // null
// Character to number
val digitChar = '5'
val digitValue = digitChar.digitToInt() // 5
// Boolean to string
val flag = true
val flagString = flag.toString() // "true"
Output:
doubleValue: 42.0
stringValue: "42"
convertedInt: 123
safeInt: null
digitValue: 5