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.
val b: Byte = 1val s: Short = 1val i: Int = 1val l: Long = 1Lval f: Float = 1.0fval d: Double = 1.0val c: Char = 'a'val isTrue: Boolean = trueval str: String = "Hello"To declare a variable that can hold a null value, append a ? to the type:
var nullableString: String? = null
nullableString = "Hello"
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 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)