Kotlin Data Types
Kotlin is a statically typed language which means a type of a variable is known at compile time.
Defining Variable with Datatype
The basic syntax for specifying a variable with a data type
val variableName: DataType = value
Let's make a couple of examples with different data types
val myNumber: Int = 10 // Int
val myDoubleNumber: Double = 4.99 // Double
val myChar: Char = 'N' // Char
val myBoolean: Boolean = false // Boolean
val myStr: String = "Hello World" // String
Let's make an another example
val myVar = 10
println(myVar)
In this code, We didn't mention the data type of variable but it is compiled successfully. That's because Kotlin have feature called Type Inference which means compiler can deduce type of expression at compile time.If you check following code you can see data type of our variable. We didn't specify any type but compiler knows type of it.
Note: You can use Kotlin Playground for prototyping
Basic Data Types in Kotlin
The following data types are supported in Kotlin. (we will explain them all with different posts)
- Numbers
- Characters
- Booleans
- Arrays
- Strings