Kotlin Data Types
Kotlin is a statically typed language. In statically typed language, the data type of the variable is known at compile time. It means, if programmer wants to define a string, it's data type must be known to compiler at compile time. Some other examples of statically typed languages are: Java, Kotlin, C, C++.
So, in Kotlin data type of a variables must be known to the compiler at compile time because it is statically typed language. But if you try to run the following code, it will execute without any error:
Here we haven't mentioned any data type for
val marks = 5 println(marks)
marksvariable, but it is executed successfully. It is because, Kotlin uses something called as Type Inference too, which means if the compiler can figure out what is the data type of the variable using type inference, then programmer need not to explicitly mention its type, the compiler will manage it itself.
We can check what all data types are automatically inferred for the declared variable, by declaring the variables like this:
val subjects = 5 println("Data type of subjects: " + subjects::class.simpleName) val marks = 47.5 println("Data type of marks: " + marks::class.simpleName) val name = "Deepak" println("Data type of name: " + name::class.simpleName)
Data type of subjects: Int
Data type of marks: Double
Data type of name: String
Data types in Kotlin
Now we will discuss about basic data types in Kotlin. Following are the basic datatypes supported:
Syntax for defining Variable with Datatype:
Here we have the basic syntax for defining a variable specifying a datatype:
val variable_name: datatype = value
So, we mostly use the
val keyword for declaring variable in Kotlin and we can specify the datatype using a colon after the variable name. We will see examples, when we will cover the various datatypes below.
1. Kotlin Numbers
Kotlin provides some built-in data types to represent numbers. Kotlin numbers can be integral values or floating point values.
For integral numbers, data type present are:
The range and space taken by each of them is given in the below table:
|Type||Size (in bytes)||Min Value||Max Value|
You can use them depending upon the required range and memory for the data that you want to store. Here's how we can declare each one of them:
val byte: Byte = 1 val short: Short = 1 val int: Int = 1 val long: Long = 1
All integral variables are inferred as
Intif they do not exceed maximum value of
To explicitly specify
Long, add suffix L to the value.
val normalNumber = 1 val explicitlyMarkedLong = 1L val longByRange = 100000000000 println("Data type of normalNumber is: " + normalNumber::class.simpleName) println("Data type of explicitlyMarkedLong is: " + explicitlyMarkedLong::class.simpleName) println("Data type of longByRange is: " + longByRange::class.simpleName)
For floating point numbers, data types provided are:
They differ in number of significant decimal digit they can have:
|Type||Size (in bytes)||Decimal digits|
val marks: Float = 1.0f val percentage: Double = 1.0
For fractional data type, compiler infers Double data type by default. For explicitly specifying Float, add suffix f or F as suffix to the value.
2. Kotlin Characters
Characters in Kotlin are declared using the Char keyword. The characters should be placed in single quotes like 'a'. The size of a char is 4 bits.
Let's take an example:
val letter: Char = 'p'
Unlike Java, a number cannot be treated as character in Kotlin:
val letter: Char = 65 // Error
3. Kotlin Booleans
A Boolean variable is used to represent two values, either true or false. Their usage is same as any other language:
val isQualified: Boolean = true val isPromoted: Boolean = false
4. Kotlin Arrays
Kotlin Arrays are the collection of values of same data types. If we want to store the marks of 7 subjects then instead of creating 7 variables, we can declare an array of size 7 and save all the numbers in it.
In Kotlin, we can declare an array using the
Array class. For example:
val marks: Array<Int> = arrayOf(10,9,2,7,8,6,10)
We will discuss about Arrays in detail in later sections.
5. Kotlin Strings
A string can be represented using the String class in Kotlin. A string is basically a collection of characters, a word or a sentence which can have characters, numbers, special characters, but everything should be enclosed within double quotes. For example, "Hi I am a string" is a string. Strings are always declared within double quotes.
Let's take an example:
val message: String = "Corona Go, Go Corona!!"
Strings are very useful while writing any program. We will cover more about String class in detail in separate tutorial.
In this tutorial, we studied about static typing and type inference in Kotlin. We also studied about various data types present in Kotlin. We will discuss about Arrays and Strings in separate tutorials in detail.