Left Index

Kotlin Data Types

In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable. Some of the types can have a special internal representation - for example, number, character and boolean can be represented as primitive values at runtime - but to the user they look like ordinary classes.


There are different data types in Kotlin:

  • Integer Data type
  • Floating Data Type
  • Boolean Data Type
  • Character Data Type

Integer Data Types

The representation of numbers in Kotlin is pretty similar to other's language like C, C++ or java etc..., however, Kotlin does not allow internal conversion of different data types. Following table lists different variable lengths for different numbers.

These data types contain integer values as:

Data Type Bits Minimum Value Maximum Value
Byte 8 -128 127
Short 16 -32768 32767
Int 32 -2147483648 2147483647
Long 64 -9223372036854775808 9223372036854775807

Let's write a program and using Integer data types:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	val byte: Byte = 2
	println("Byte value: " + byte + ", min value: " + Byte.MIN_VALUE + ", max value: " + Byte.MAX_VALUE)

	val short: Short = 10
	println("Short value: " + short + ", min value: " + Short.MIN_VALUE + ", max value: " + Short.MAX_VALUE)

	val int: Int = 10
	println("Integer value: " + int + ", min value: " + Int.MIN_VALUE + ", max value: " + Int.MAX_VALUE)

	val long: Long = 10
	println("Long value: " + long + ", min value: " + Long.MIN_VALUE + ", max value: " + Long.MAX_VALUE)
}
Output
Byte value: 2, min value: -128, max value: 127
Short value: 10, min value: -32768, max value: 32767
Integer value: 10, min value: -2147483648, max value: 2147483647
Long value: 10, min value: -9223372036854775808, max value: 9223372036854775807

Floating Data Types

Following table contains floating data types with maximum and minimum values:

Data Type Bits Min Value Max Value
Float 32 1.4E-45 3.4028235E38
Double 64 4.9E-324 1.7976931348623157E308

Let's write a program and using Integer data types:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	val float: Float = 34.9f
	println("Float value: " + float + ", min value: " + Float.MIN_VALUE + ", max value: " + Float.MAX_VALUE)
	
	val double: Double = 34.9
	println("Double value: " + double + ", min value: " + Double.MIN_VALUE + ", max value: " + Double.MAX_VALUE)
}
Output:
Float value: 34.9, min value: 1.4E-45, max value: 3.4028235E38
Double value: 34.9, min value: 4.9E-324, max value: 1.7976931348623157E308

Boolean Data Type

Boolean data type contains only one of two value i.e. true or false and contain only 1 bit. Let's see an example to demonstrate Boolean:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	val bool: Boolean = true;
	println("Boolean value: " + bool)
}
Output
Boolean value: true

Character Data Type

Character data type represents the letters(a-z or A-Z, 0-9) and other symbols, those can fit into 8 bit, because Character type have only 8 bit size only. Let's create an example with Character data type:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	val char: Char = 'a'
	println("Character value: " + char)
}
Output:
Character value: a