Kotlin Data Types Defined: In-Depth Explanation:
Kotlin is a modern programming language, created by JetBrains, that increases developer productivity and provides a safe coding approach.
The first concept that every programming language has is their data types, which are the possible values that can be stored and manipulated. In this article, we will go through the data types in Kotlin including primitive and non-primitive, type inference nullable types, etc.
- Primitive Data Types :
Kotlin offers several primitive data types that form the building blocks of data manipulation. These types are efficient and straightforward to use. Here are the key primitive data types in Kotlin:
Int is a 32-bit signed integer used for whole numbers. Example: Val age: Example Val age: Int = 25
Long: A 64-bit signed integer suitable for large whole numbers.
Example: Val distance: Long = 100000L
Short: A 16-bit signed integer, effective for saving memory in large arrays when smaller numbers are needed.
Example: Val temperature: Short = 30
Byte: An 8-bit signed integer, used for small numerical values.
Example: Val flag: Byte = 1
Double: A 64-bit double-precision floating-point number for representing decimal values.
Example: Val price: Double = 19.99
Float: A 32-bit single-precision floating-point number, less precise than Double.
Example: Val pi: Float = 3.14F
Boolean: A type that represents truth values, either true or false.
Example: Val isKotlinFun: Boolean = true
Char: A single 16-bit Unicode character.
Example: Val initial: Char = ‘K’
2. Non-Primitive Data Types:
In addition to primitive types, Kotlin also supports several non-primitive data types, which provide more complex ways to manage data:
String: A sequence of characters enclosed in double quotes.
Example: val name: String = “Kotlin”
Array: A collection of elements that are all of the same type.
Example: Val numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5)
List: An ordered collection that can contain duplicate elements.
Example: Val list = listOf(1, 2, 3, 4, 4)
Set: A collection that holds unique values, with no duplicates allowed.
Example: Val set = setOf(1, 2, 3, 3)
Map: A collection of key-value pairs, where each key is unique.
Example: Val map = mapOf(“key1” to “value1”, “key2” to “value2”)
3. Type Inference
Type inference in Kotlin allows the compiler to automatically determine the type of a variable based on its initializer. This feature reduces verbosity and enhances code readability, enabling developers to write less boilerplate code.
4. Nullable Types
Kotlin’s type system specifically categorizes nullable and non-nullable types, significantly reducing the chances of encountering NullPointerExceptions. By default, types in Kotlin cannot hold null. To indicate that a variable can hold a null value, add the ? suffix to the type declaration
Kotlin provides several mechanisms for safely handling nullable types, such as safe calls (‘?.’), the Elvis operator (‘?:’), and the not-null assertion operator (‘!!’).