🔢 Kotlin – Data Types: Understand Kotlin’s Type System
🧲 Introduction – Why Learn Kotlin Data Types?
In Kotlin, every variable has a type, and understanding these types is essential for writing safe, predictable, and optimized code. Kotlin’s type system is statically typed, meaning types are known at compile time. It supports both primitive and reference types, making it ideal for both Android development and backend systems.
🎯 In this guide, you’ll learn:
- The built-in data types in Kotlin
- Syntax for declaring and using types
- Difference between nullable and non-nullable types
- Type conversions and best practices
🧾 Kotlin Data Type Categories
Kotlin provides a rich set of data types, grouped as follows:
Category | Types |
---|---|
🧮 Numeric | Byte , Short , Int , Long , Float , Double |
🔤 Character/Text | Char , String |
✅ Boolean | Boolean (true or false ) |
❔ Nullable | Nullable types using ? (e.g., String? ) |
📦 Collections | List , Set , Map , Arrays |
🧠 Numeric Data Types in Kotlin
Type | Size | Example |
---|---|---|
Byte | 8-bit | val b: Byte = 127 |
Short | 16-bit | val s: Short = 1000 |
Int | 32-bit | val i: Int = 123456 |
Long | 64-bit | val l: Long = 123456789L |
Float | 32-bit | val f: Float = 12.34F |
Double | 64-bit | val d: Double = 123.456 |
📌 Use suffix L
for Long
and F
for Float
.
🔤 Character and String Types
val grade: Char = 'A'
val message: String = "Kotlin is fun!"
Char
stores a single character inside single quotes.String
stores a sequence of characters inside double quotes.
val name = "Riya"
println("Hello, $name") // String template
✅ Boolean Type
val isActive: Boolean = true
val isAdmin = false
- Use
Boolean
for logical conditions. - Accepts only
true
orfalse
.
❔ Nullable Types in Kotlin
Kotlin avoids NullPointerExceptions
using null safety with ?
.
var name: String? = null
name = "Sam"
println(name?.length) // Safe call
String?
can hold anull
value.- Use
?.
for safe calls or!!
for force-unwrapping.
🔁 Kotlin Type Inference
Kotlin can automatically determine the variable type:
val age = 30 // Inferred as Int
val price = 19.99 // Inferred as Double
val title = "Book" // Inferred as String
💡 You can explicitly declare types if needed for clarity or API requirements.
🔄 Type Conversion in Kotlin
Use conversion functions for safe type casting:
val x: Int = 10
val y: Double = x.toDouble()
val str: String = x.toString()
From | To | Method |
---|---|---|
Int | Double | toDouble() |
Float | Int | toInt() |
Any | String | toString() |
⚠️ Kotlin does not support implicit type casting.
🧪 Kotlin Type Checking and Smart Casts
fun getLength(obj: Any) {
if (obj is String) {
println(obj.length) // Smart cast to String
}
}
- Kotlin uses
is
and!is
for type checking. - Smart casts apply when Kotlin knows the type won’t change.
🚫 Common Mistakes with Kotlin Data Types
❌ Mistake | ✅ Solution |
---|---|
Assigning null to non-nullable type | Declare it as String? |
Using == null without null check | Use safe call ?. or null-check block |
Mixing Float and Int without cast | Use toFloat() , toInt() etc. |
📌 Summary – Recap & Next Steps
Kotlin’s data types are concise, null-safe, and type-strict, giving you both safety and flexibility. By understanding how each type works and how to convert or check them, you can build reliable and efficient applications.
🔍 Key Takeaways:
- Kotlin supports primitive and reference types like
Int
,String
, andBoolean
. - Use
?
to declare nullable types and avoid crashes. - Kotlin requires explicit type conversions.
- Type inference simplifies declarations, but static typing remains.
⚙️ Practical Use:
Use data types to define variables, handle logic, manage nulls safely, and perform efficient computations in Kotlin projects like Android apps, APIs, or backend systems.
❓ FAQs – Kotlin Data Types
❓ Does Kotlin support primitive types like Java?
✅ Yes. Kotlin uses wrapper classes (Int
, Double
, etc.) but compiles to JVM primitives under the hood for performance.
❓ What is a nullable type in Kotlin?
✅ A type that can hold null
values. You declare it using ?
, like String?
.
❓ Can Kotlin infer data types automatically?
✅ Yes. Kotlin uses smart type inference but keeps type safety at compile time.
❓ How do I convert types in Kotlin?
✅ Use conversion methods like toInt()
, toDouble()
, toString()
explicitly.
❓ What’s the difference between Char
and String
in Kotlin?
✅ Char
stores a single character (e.g., 'A'
), while String
stores a sequence of characters (e.g., "Hello"
).
Share Now :