Basic DataTypes in Kotlin
In Kotlin, each variable and data structure has a type. These kinds are crucial because they inform the compiler of the operations that can be performed on them. To put it another way, the characteristics of a variable or data structure are determined by its type.
Just like Java, we don’t need to declare a variable’s type during declaration. Kotlin infers the variable’s type, and this ability is called type inference. So, if we declare a variable like
val customers = 10 // it means the customer is assigned an integer valueKotlin infers that customers can execute arithmetic operations and have a numerical data type. For example.
fun main() {
var customers = 10
// since it's a var so we can re-assign it
customers = 100
// example of arithmetic operations
// 1. addition
customers = customers + 10 // results to 110
/**
* instead of writing the whole expression we can shorten everything using
* augmented assignment operators
* +=, -=, *=, /=, %=
*/
customers += 10 // this results to 120
// 2. subtraction
customers -= 30 // this results to 90
// 3. multiply
customers *= 5 // this results to 450
// 4. division
customers /= 4 // this results to 112 (which is close to 112.5)
println(customers)
}Why did we receive 112 instead of 112.5? The short explanation is that Kotlin makes decisions based on the initial value we set to a variable. It reads the type as a decimal value if we assign 10.5, the outcome is 112.5 rather than 112.
Get Rajesh Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
The other types in Kotlin are:
With this basic information, we can start declaring variables and understand the types of declared variables.







