In this tutorial, you shall learn about datatypes Kotlin language, different primitive datatypes available, with examples.
Kotlin Datatypes
Datatype decides the type of values we can store in a variable.
For example, if a variable is declared as an integer, we can store only integer values in it, not String, not boolean, nor any other incompatible datatypes.
Kotlin provides many different datatypes via inbuilt classes. These datatypes can be categorised into five. They are
- Numbers
- Booleans
- Characters
- Strings
- Arrays
Numbers
There are six different datatypes which can store numbers. They differ in the size allotted to them in memory, or the precision of floating point.
- Byte
- Short
- Int
- Long
- Float
- Double
Of these first four store only integers. Last two store floating point numbers.
The following tables provides information on the number of bytes a datatype requires, and the range of values a specific datatype can store.
Datatype | Size (Bytes) | Min Value : Max Value |
---|---|---|
Byte | 1 | -128 : 127 |
Short | 2 | -32768 : 32767 |
Int | 4 | -2,147,483,648 : -2,147,483,647 |
Long | 8 | -9,223,372,036,854,775,808 : 9,223,372,036,854,775,807 |
Datatype | Size (Bytes) | Decimal Digits |
---|---|---|
Float | 4 | 6 to 7 |
Double | 8 | 15 to 16 |
Byte, Short, Int, Double
By default, any integer number would be considered Int. If the integer exceeds the range of Int, then it would be considered Long.
In the following example, we assign integer values to variables x and y. If the range of the integer value is greater than Int, then it would be considered Long.
Main.kt
fun main(args: Array<String>) {
val x = 2567
println("Datatype of x is ${x.javaClass.kotlin.qualifiedName}")
val y = 256755896454754859
println("Datatype of y is ${y.javaClass.kotlin.qualifiedName}")
}
Output
Datatype of x is kotlin.Int
Datatype of y is kotlin.Long
Since, the value stored in y
is too big for an Int value, Kotlin infers it to a Long value.
To explicitly specify the numeric type for a variable, we may provide the type in the declaration.
In the following example, we explicitly specify the datatype for variables.
Main.kt
fun main(args: Array<String>) {
val a: Byte = 25
println("Datatype of a is ${a.javaClass.kotlin.qualifiedName}")
val b: Short = 35
println("Datatype of b is ${b.javaClass.kotlin.qualifiedName}")
val c: Int = 45
println("Datatype of c is ${c.javaClass.kotlin.qualifiedName}")
val d: Long = 55
println("Datatype of d is ${d.javaClass.kotlin.qualifiedName}")
}
Output
Datatype of a is kotlin.Byte
Datatype of b is kotlin.Short
Datatype of c is kotlin.Int
Datatype of d is kotlin.Long
For Long values, we may also specify the literal L after the value, without specifying the class type for variable.
val d = 55L
Float, Double
Any decimal point value assigned to a variable, without any explicit type declaration, would be considered a Double value.
Main.kt
fun main(args: Array<String>) {
val a = 25.25
println("Datatype of a is ${a.javaClass.kotlin.qualifiedName}")
val b = 35.258896445887545
println("Datatype of b is ${b.javaClass.kotlin.qualifiedName}")
}
Output
Datatype of a is kotlin.Double
Datatype of b is kotlin.Double
In the above program, the value of a
is well within the range of a float, but since Kotlin considers any decimal point value as Double, both a
and b
are considered Double values.
To create a Float variable, provide the literal f
or F
after the value.
Examples
val a = 25.25F
val b = 25.25f
Boolean
Boolean datatype is implemented via Boolean class in Kotlin. Boolean variable can store either of the two values: true
, false
.
To declare a Boolean variable, use Boolean class name.
val a: Boolean
Or, we may assign a boolean value to the variable during the declaration itself, without explicitly specifying the Boolean class name.
val a = true
Character
Character datatype is implemented via Char class in Kotlin. Char variable can store a single character.
To declare a Char variable, use Char class.
val c: Char
Or, we may assign a character value to the variable during the declaration itself, without explicitly specifying the Char class. Kotlin can infer the type from the value assigned to variable.
val c = 'T'
Character values are enclosed in single quotes.
String
String datatype is implemented via String class in Kotlin. String variable can store a sequence of characters.
To declare a String variable, use String class.
val a: String
Or, we may assign a string value to the variable during the declaration itself, without explicitly specifying the String class. Kotlin can infer the type from the value assigned to variable.
val a = "Hello World"
String values are enclosed in double quotes.
Array
Array datatype is implemented via Array class in Kotlin. Array variable can store similar datatype items.
More about arrays in Kotlin Array tutorial.
Conclusion
In this Kotlin Tutorial, we learned different datatypes in Kotlin, their size in bytes, how to create them, etc., with the help of examples.