It specifies the type of data variable can store like data could be string, integer, numeric, boolean, float.
In kotlin all data types behave as object in java need to use wrappers classes(like java.lang.Integer) to behave like objects. you can call member function and properties on any variable.
Data types in Kotlin:
- Integer Data type
- Floating-point Data Type
- Boolean Data Type
- Character Data Type
- Strings Data Type
- Arrays
1.Integer Data Type – used to store integer values, these data types can store integer data.
Byte: it’s used when you know the value will be within -128 and 127 . it’s used to save memory.
val num: Byte = 10
println(num)
Short : it can store whole numbers when value be within -32768 to 32767:
val num: Short= 2000
println(num)
Int : it can store whole numbers when value be within -2147483648 to 2147483647
val num: Int = 100000
println(num)
Long : The Long data type can store whole numbers from -9223372036854775807 to 9223372036854775807. You can end the value with an L.
val num: Long = 15000000000L
println(num)
Difference Between Int and Long
If the value be within -2147483648 to 2147483647 it’s defined as Int type. when the value is greater than 2147483647. it’s defined as Long type.
val number1 = 2147483647 // Int
val number2 = 2147483648 // Long
Data Type | Bits | Min Value | Max Value |
Byte | 8 bits | -128 | 127 |
Short | 16 bits | -32768 | 32767 |
Int | 32 bits | -2147483648 | 2147483647 |
long | 64 bits | -9223372036854775808 | 9223372036854775807 |
2. Floating-point Data Type – it’s used to store number with decimal values, like 1.23 or 1.34343. Float and Double data types is used to store fractional numbers.
Data Type | Bits | Min Value | Max Value |
Float | 32 bits | 1.40129846432481707e-45 | 3.40282346638528860e+38 |
Double | 64 bits | 4.94065645841246544e-324 | 1.79769313486231570e+308 |
val num1: Float = 5.75F // you can end the value with F
println(num1)
val num2: Double = 19.99
println(num2)
Difference Between Float or Double:
Double is more precise than Float and can store 64 bits and float can store 32 bits. Float can have a precision of up to 6 decimal places and Double can have a precision of up to 15 decimal places
precision of a floating point value represents how many digits the value can have after the decimal points.
3. Char Data Type : It is used to declare the character values. It is a single 16-bit Unicode Character with a value range of 0 to 65,535 (inclusive). it’s value must be surrounded by single quotes
Data Type | Bits | Min Value | Max Value |
Char | 4 bits | -128 | 127 |
Char character = 'H'
println(character) // H
4. Boolean Data Type : it’s used to store only one bit of information either true or false.
fun main(args : Array<String>){
var flag = true
if (flag){
print("flag is true")
}
}
5. String Data Type : It uses to store sequence of characters and must be surrounded by double Quotes
val name: String = "Coding Friction"
println(name)
6. Arrays : It is uses to store multiple values of same type in single variable instead of declaring separates variables.