Kotlin Variables and Data Types
Define Variables
In Kotlin, like many other programming languages, we use variables to store and manipulate data. These variables act as containers that hold individual pieces of data. The size of each container depends on the data type it stores, which determines the amount of memory allocated to it.
To define variables in Kotlin, we use two keywords:
- val: This keyword is used to create read-only variables, often referred to as immutable. While the value stored in a val variable cannot be changed after assignment, these variables are not strictly immutable in the sense that their internal state cannot be modified.
- var: This keyword is used to create mutable variables, which allow you to change the value stored in the variable after its initial assignment.
After these keywords, you need to assign a meaningful name to your variable. Specifying the data type is generally optional, as Kotlin can often infer the type based on the assigned value. Like below , even we don’t specify surname’s and gravity’s types , they inferred as String and Double automatically.
In Kotlin every thing is an object and we can say that on the contrary of Java , Kotlin has no primitive types.However in conversion from Kotlin code to bytecode , with some optimisations primitive types are converting as primitive java equivalances.
Compare our example and it’s decompiled version.You will see our varibles converted as Primitive Types.
1 | import kotlin.Metadata; |
Why val is not immutable but readonly ?
The value you’re assigned to the variable is used to create a new object.If you declare the variable using val, the reference to the object stays in the variable forever and can’t be replaced. You can see availableSpace value depends to other variables value.Because of that is not immutable but read-only.
For more read Paolo Montalto's postKotlin has the same standard types that you’d expect from a mainstream programming language. These are Numbers, Characters , Booleans, Arrays and String.
Numbers Data Types
Number types are divided into two groups , Integer types and Floating Types
Integer Types
Type | Size (bits) | Min value | Max value |
---|---|---|---|
Byte | 8 | -128 | 127 |
Short | 16 | -32768 | 32767 |
Int | 32 | -2,147,483,648 (-231) | 2,147,483,647 (231 - 1) |
Long | 64 | -9,223,372,036,854,775,808 (-263) | 9,223,372,036,854,775,807 (263 - 1) |
Floating Point Types
Type | Size (bits) | Min value | Max value |
---|---|---|---|
Float | 32 | 1.40129846432481707e-45 | 3.40282346638528860e+38 |
Double | 16 | 4.94065645841246544e-324 | 1.79769313486231570e+308 |
Character Types
Type | Size (bits) | Min value | Max value |
---|---|---|---|
Char | 4 | -128 | 127 |
Boolean Types
Type | Size (bits) | Value |
---|---|---|
Boolean | 1 | true or false |
Arrays
Arrays in Kotlin, like in many other programming languages, are a fundamental data structure that can be used to store multiple values of the same type in a single variable. Here are some reasons why we might use arrays in Kotlin:
Efficient Storage: Arrays allow you to store multiple items of the same type in a single variable, which can make your code more efficient and easier to read.
Indexed Access: Each element in an array has a unique index, starting from 0. This allows for quick access to any element in the array, which can be very useful in certain algorithms.
Iteration: Arrays can be easily iterated over, which is useful when you want to perform an operation on each element in the collection.
Fixed Size: Arrays have a fixed size once they are created. This can be useful when you know exactly how many elements you need to store.
Interoperability: Kotlin’s Array class provides interoperability with Java and other JVM languages. This is important when working in a mixed-language codebase.
Here’s a simple example of how to declare and initialize an array in Kotlin:
val numbers = arrayOf(1, 2, 3, 4, 5)
And here’s how you might access elements and iterate over an array:
val firstNumber = numbers[0] // Access the first element
for (number in numbers) { // Iterate over the array
println(number)
}
Remember, while arrays can be very useful, they are not always the best tool for the job. Kotlin also provides other collection types, like List, Set, and Map, which might be more appropriate depending on your specific needs.
In Kotlin, there are two types of arrays: primitive arrays and class arrays.
Primitive Arrays:
- Primitive arrays are used to store elements of primitive data types such as Int, Long, Float, etc.
- These arrays are more memory-efficient as they directly store the values in a contiguous memory block.
- Primitive arrays cannot store null values. They always have default values based on the data type (e.g., 0 for Int, false for Boolean).
- Primitive arrays have a fixed size that cannot be changed once created.
Class Arrays:
- Class arrays are used to store elements of class types or nullable types.
- Class arrays store references to objects rather than the actual values.
- Class arrays can store null values.
- Class arrays have a dynamic size and can be resized using functions like
Array.copyOf()
orArray.resize()
.
Here’s an example to illustrate the difference:
In the above example, primitiveArray
is a primitive array of type IntArray
that stores integer values directly. On the other hand, classArray
is a class array of type Array<Int?>
that stores references to Int
objects, including a null value.
It’s important to choose the appropriate array type based on your requirements to optimize memory usage and handle nullable values effectively.