KOTLIN Tutorial



DATA TYPES IN KOTLIN


Data Types in Kotlin

Kotlin is a statically typed language, meaning every variable and expression has a type. Kotlin's type system is designed to help you write safe and reliable code while reducing boilerplate code.

Note:

Kotlin supports a rich set of built-in data types, and these types can be broadly classified into two categories: primitive types and reference types.

Primitive Data Types

In Kotlin, primitive types are implemented as objects in the underlying JVM. However, they behave as primitives in most operations for performance reasons. Here are the main primitive data types in Kotlin:

  • Byte: 8-bit signed integer, ranging from -128 to 127.
  • Short: 16-bit signed integer, ranging from -32,768 to 32,767.
  • Int: 32-bit signed integer, ranging from -2,147,483,648 to 2,147,483,647.
  • Long: 64-bit signed integer, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • Float: 32-bit floating point number, used for decimals.
  • Double: 64-bit floating point number, used for decimals.
  • Char: A single 16-bit Unicode character.
  • Boolean: Represents a value of true or false.

Reference Types

Reference types in Kotlin include all objects and arrays. Any instance of a class, interface, or array is a reference type. These types are stored as references in memory, not directly as values.

Example: Working with Kotlin Data Types

Here’s an example that shows how to declare variables using different data types in Kotlin:

fun main() {
    // Primitive types
    val byteValue: Byte = 100
    val shortValue: Short = 2000
    val intValue: Int = 50000
    val longValue: Long = 100000L
    val floatValue: Float = 10.5F
    val doubleValue: Double = 20.99
    val charValue: Char = 'K'
    val booleanValue: Boolean = true

    println("Byte Value: $byteValue")
    println("Short Value: $shortValue")
    println("Int Value: $intValue")
    println("Long Value: $longValue")
    println("Float Value: $floatValue")
    println("Double Value: $doubleValue")
    println("Char Value: $charValue")
    println("Boolean Value: $booleanValue")
}
    

Nullable Data Types

In Kotlin, variables can hold null values. You can make a type nullable by adding a question mark (?) after the type name. For example, String? means that the variable can hold either a string or null.

fun main() {
    // Nullable data type
    val name: String? = null
    if (name != null) {
        println("Name length: ${name.length}")
    } else {
        println("Name is null")
    }
}
    

Type Inference in Kotlin

Kotlin uses type inference to automatically determine the type of a variable based on the initializer. You don’t always need to specify the type explicitly.

fun main() {
    val number = 42  // Type inferred as Int
    val pi = 3.14  // Type inferred as Double
    println("Number: $number")
    println("Pi: $pi")
}
    

Conclusion

Kotlin provides a rich set of built-in data types for both primitive and reference values. Understanding these types, as well as how to use nullable types and type inference, is essential for writing effective Kotlin code. Use the appropriate data type for each scenario to ensure your code is efficient and readable.

Quick Tip:

Use nullable types when you expect a value might be absent, but be sure to handle null values properly to avoid runtime errors.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review