DATA TYPES

In Kotlin, data types represent the kind of values that variables can hold. Kotlin provides a rich set of built-in data types that cover most of the needs for any application. Let's explore these data types and see different ways to declare and initialize them.

Primitive Data Types

  1. Integer Types:
  2. Floating Point Types:
  3. Character Type:
  4. Boolean Type:
  5. String Type:

Nullable Types

To declare a variable that can hold a null value, append a ? to the type:

var nullableString: String? = null
nullableString = "Hello"

Type Inference

Kotlin supports type inference, meaning the type can often be omitted if the compiler can infer it from the context:

val inferredInt = 1      // Compiler infers Int
val inferredString = "Hello"  // Compiler infers String

Arrays

Arrays can be declared using the arrayOf function or with specialized types for primitive arrays:

val array = arrayOf(1, 2, 3)
val intArray = intArrayOf(1, 2, 3)

Collections