Kotlin Data Types

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 TypeBitsMin ValueMax Value
Byte8 bits-128127
Short16 bits-32768       32767
Int32 bits -21474836482147483647
long64 bits-92233720368547758089223372036854775807

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 TypeBitsMin ValueMax Value
Float32 bits1.40129846432481707e-453.40282346638528860e+38
Double64 bits4.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 TypeBitsMin ValueMax Value
Char4 bits-128127
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.

Leave a Reply