Kotlin Syntax & Language Basics
Estimated reading: 4 minutes 45 views

🔢 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:

CategoryTypes
🧮 NumericByte, Short, Int, Long, Float, Double
🔤 Character/TextChar, String
✅ BooleanBoolean (true or false)
❔ NullableNullable types using ? (e.g., String?)
📦 CollectionsList, Set, Map, Arrays

🧠 Numeric Data Types in Kotlin

TypeSizeExample
Byte8-bitval b: Byte = 127
Short16-bitval s: Short = 1000
Int32-bitval i: Int = 123456
Long64-bitval l: Long = 123456789L
Float32-bitval f: Float = 12.34F
Double64-bitval 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 or false.

❔ 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 a null 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()
FromToMethod
IntDoubletoDouble()
FloatInttoInt()
AnyStringtoString()

⚠️ 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 typeDeclare it as String?
Using == null without null checkUse safe call ?. or null-check block
Mixing Float and Int without castUse 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, and Boolean.
  • 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin – Data Types

Or Copy Link

CONTENTS
Scroll to Top