Kotlin's built-in data type can be categorized as follows:
Number
Character
String
Boolean
Array
Number Data Types
Kotlin number data types are used to define variables that hold numeric values and they are divided into two groups:
(a) Integer data store whole numbers, positive or negative
(b) Floating data types represent numbers with a fractional part, containing one or more decimals.
Data Type | Size (bits) | Data Range |
Byte | 8 bit | -128 to 127 |
Short | 16 bit | -32768 to 32767 |
int | 32 bit | -2,147,483,648 to 2,147,483,647 |
longs | 64 bit | -9,223,372,036,854,775,808to+9,223,372,036,854,775,807 |
float | 32 bit | 1.40129846432481707e-45to3.40282346638528860e+38 |
Double | 64 bit | 4.94065645841246544e-324 to1.79769313486231570e+308
|
examples:
fun main(args: Array<String>)
{
val a: Int =10000
val d: Double =100.00
val f: Float =100.00f
val l: Long =1000000004
val s: Short =10
val b: Byte =1
println("Int Value is "+ a)
println("Double Value is "+ d)
println("Float Value is "+ f)
println("Long Value is "+ l )
println("Short Value is "+ s)
println("Byte Value is "+ b)
}
Output:
Int Value is 10000
Double Value is 100.0
Float Value is 100.0
Long Value is 1000000004
Short Value is 10
Byte Value is 1
Character Data Type
Kotlin character data type is used to store a single character and they are represented by the type Char keyword. A Char value must be surrounded by single quotes, like 'A' or '1'.
fun main(args: Array<String>)
{
val letter: Char
letter ='A'
println("$letter")
}
Output:
A
String Data Type
The String data type is used to store a sequence of characters. String values must be surrounded by double quotes (" ") or triple quote (""" """).
We have two kinds of string available in Kotlin - one is called Escaped String and another is called Raw String.
Escaped string is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' etc.
Raw string is declared within triple quote (""" """) and may contain multiple lines of text without any escape characters.
funmain(args: Array<String>){
val escapedString : String ="I am escaped String!\n"
var rawString :String ="""This is going to be a multi-line string and will not have any escape sequence""";
print(escapedString)
println(rawString)}
Output:
I am escaped String!
This is going to be a
multi-line string and will
not have any escape sequence
Boolean Data Type
Boolean is like other programming languages. We have only two values for the Boolean data type - either true or false.
fun main(args: Array<String>)
{
val A: Boolean = true
B: Boolean = false
println("Value of variable A "+ A )
println("Value of variable B "+ B )
}
Output:
Value of variable A true
Value of variable B false
Array Data Type
Kotlin arrays are a collection of homogeneous data. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
We will study array in a separate chapter, for now let's look at one example to define an array of integers and then access its one of the elements.
fun main(args: Array<String>) {
val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
println("Value at 3rd position : " + numbers[2])
}
Output:
Value at 3rd position : 3
Hope you enjoy learning about data type in kotlin. In the next blog, we are going to learn about nullability or null safety in kotlin Thank you
The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!
Comments