Kotlin Datatypes
In this article, you will learn different data types of kotlin with examples.
5. Kotlin Datatypes
5.1 Kotlin datatypes with examples
5.2 Check data type
Kotlin datatypes with examples
Value store in a variable is of some data type, it specifies how much space it occupies in memory location. As the kotlin is statically typed language so, the type of a variable is known at the compile time. There are basically five data types in Kotlin.
- Number
- Boolean
- Char
- String
- Array
Number
These are the different number types in kotlin with size limit.
Type | Width | Description |
Byte | 8 | It is used where the value of a variable within [-128, 127]. |
Short | 16 | It is used where the value of a variable within [-32768, 32767]. |
Int | 32 | It is 32 bit signed 2's complement integer. |
Float | 32 | It is 32 bit single-precision floating point. |
Long | 64 | It is 64 bit signed 2's complement integer. |
Double | 64 | It is 64 bit double-precision floating point. |
Example of Number
fun main(args : Array<String>) {
val a = 22 //int
val l = 34343L //long
val d = 43.341 //double
val f = 33.12F //float
val hx = 0x64 //hexadecimal
val b = 110010 //binary
println("$a");
println("$l");
println("$d");
println("$f");
println("$hx");
println("$b");
}
Output
22
34343
43.341
33.12
100
110010
In the above example, there is no need to mention the type of variables, the compiler knows that the variable a is of type int, d is of type double.
Boolean
Boolean has only two possible values either TRUE or FALSE. Booleans are used in many condition code statements. Comparison operator always returns a boolean value.
Example of Boolean
fun main(args : Array<String>) {
val a = 10
val b = 5
val c = 8
if(a < b && a > c)
println("True")
else
println("False")
}
Character
The character data type is represented by char. It is represented by single or double quotes.
Example of Chars
fun main(args : Array<String>) {
val chr: Char
chr = 'E'
println("The character is $chr")
}
Output
The character is E
We can escape special characters using a backslash, like - \t, \b, \n, \r, \", \\ and \$.
String
The string is a sequence of characters enclosed within double and triple quotes. It is immutable means you cannot change the individual character of a string. It is represented by the type String.
Example of String
fun main(args : Array<String>) {
val str1 = "Welcome to this tutorial."
var str2 = """This is envisioned and developed to meet basic and free courses to learn, succeed and belong."""
println("$str1 $str2")
}
We can iterate over a string using for loop.
fun main(args : Array<String>) {
val str = "Welcome to this tutorial."
for (s in str) {
println(s)
}
}
Output
W
e
l
c
o
m
e
t
o
t
h
i
s
t
u
t
o
r
i
a
l
.
A string may contain escaped characters in them.
fun main(args : Array<String>) {
val s = "Hello, John!\n"
}
Array
Kotlin provides two ways to create arrays - arrayOf() and array() constructor.
Example of arrayOf()
val array1 = arrayOf(22,12,43)
Example of array() constructor
val array2 = Array(10, 'Smith', 'Jorz')
Check kotlin data type
The is operator checks whether an object is of the given type at runtime.
fun main(args : Array<String>) {
var s1 = "Hello World"
fun getStrLength(s1: Any):Int?{
if(s1 is String) {
return s1.length
}
return null
}
}