Kotlin is a statically typed language, which means that variables must be declared with a specific data type before they can be used. Here are the basic data types available in Kotlin:
Numeric types: Kotlin supports various numeric types, including
Double. These types are used to represent different kinds of numbers with varying precision and size.
val byteVal: Byte = 127 val shortVal: Short = 32767 val intVal: Int = 2147483647 val longVal: Long = 9223372036854775807 val floatVal: Float = 3.14f val doubleVal: Double = 3.14159
- Byte: A
Byteis an 8-bit signed integer that can store values between -128 and 127. It is useful for working with small numbers that don’t require a lot of memory.
- Short: A
Shortis a 16-bit signed integer that can store values between -32768 and 32767. It is useful for working with larger numbers than a
Byte, but still within a relatively small range.
- Int: An
Intis a 32-bit signed integer that can store values between -2^31 and 2^31-1. It is the most commonly used numeric type in Kotlin, and is suitable for most use cases.
- Long: A
Longis a 64-bit signed integer that can store values between -2^63 and 2^63-1. It is useful for working with very large numbers that cannot be represented by an
- Float: A
Floatis a 32-bit floating-point number that can store values with up to 7 digits of precision. It is useful for working with numbers that require decimal points, but don’t require high precision.
- Double: A
Doubleis a 64-bit floating-point number that can store values with up to 15-16 digits of precision. It is the default choice for decimal numbers and is suitable for most use cases.
You can use these numeric types in Kotlin to perform mathematical calculations, store numbers in variables, and work with different kinds of data. It is important to choose the appropriate numeric type for your variables, based on the range and precision of the values you need to store. Using a smaller numeric type when a larger one is not necessary can help to conserve memory and improve performance.
Boolean type: The
Boolean type is used to represent a value that can be either true or false.
val isStudent: Boolean = true val isEmployee: Boolean = false
Character type: The
Char type is used to represent a single character.
val letter: Char = 'A'
String type: The
String type is used to represent a sequence of characters.
val message: String = "Hello, world!"
Array type: The
Array type is used to represent a collection of elements of the same type.
val numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5) val names: Array<String> = arrayOf("Alice", "Bob", "Charlie")
Nullability: In Kotlin, all data types can be declared nullable by adding a question mark (
?) to the end of the type declaration. This means that the variable can either hold a value of the specified type or a null value.
val nullableString: String? = null val nullableInt: Int? = 42
Understanding these basic data types in Kotlin is important for writing effective and efficient code. By choosing the appropriate data type for a variable, you can ensure that the variable can hold the necessary value and optimize the performance of your code.