Data types in Kotlin

In Kotlin, data types can be broadly categorized into two main groups: primitive types and reference types. Here’s an overview of the data types available in Kotlin:

Primitive types

Kotlin has a rich set of built-in primitive data types that are used for storing simple values.

Numbers

Data Type

Description

Range

Byte

8-bit signed integer

-128 to 127

Short

16-bit signed integer

-32768 to 32767

Int

32-bit signed integer

-2^31 to 2^31 - 1

Long

4-bit signed integer

2^63 to 2^63 - 1

Float

32-bit floating point number

1.40129846432481707e-45 to 3.40282346638528860e+38

Double

64-bit floating point number

4.94065645841246544e-324 to 1.79769313486231570e+308

The coding example of number data type in Kotlin is as follows:

fun main() {
// Primitive types
val intNumber: Int = 42
val doubleNumber: Double = 3.14
println(intNumber)
println(doubleNumber)
}

Characters

The Char data type represents a single 16-bit Unicode character. The coding example of character data type in Kotlin is as follows:

fun main() {
// Primitive types
val char: Char = 'A'
println(char)
}

Boolean

The Boolean data type represents a boolean value, either true or false. The coding example of boolean data type in Kotlin is as follows:

fun main() {
// Primitive types
val boolean: Boolean = true
println(boolean) // Use the variable to avoid the warning
}

Reference types

  • Arrays: Arrays in Kotlin are represented by the Array class. Arrays can hold elements of a specific data type.

  • Strings: Strings are represented by the String class. They are immutable sequences of characters.

  • Classes and objects: Kotlin is an object-oriented language, so you can define your own classes and create objects from them.

  • Functions: Functions are first-class citizens in Kotlin and can be assigned to variables, passed as arguments, and returned from other functions.

  • Collections: Kotlin provides a rich set of collection classes in the standard library, including lists, sets, maps, and more.

Additionally, Kotlin also supports nullable types, represented by appending ? to the type declaration. This allows variables to hold a null reference in addition to the specified type.

Coding example of all data types of Kotlin

The below code snippet demonstrates the declaration and usage of various data types in Kotlin, including primitive types, reference types, and nullable types.

fun main() {
// Reference types
val string: String = "Hello, Kotlin!"
val array: Array<Int> = arrayOf(1, 2, 3, 4, 5)
// Nullable types
val nullableString: String? = null
val nullableInt: Int? = null
println(string)
println(array.joinToString())
println(nullableString)
println(nullableInt)
}

Explanation

Here’s a concise line-by-line explanation of the Kotlin code:

  • Line 1: Defines the main function, the entry point of the program.

  • Line 3: Declares a non-nullable String variable named string with the value “Hello, Kotlin!”.

  • Line 4: Declares an Array<Int> variable named array initialized with integers 1 to 5.

  • Line 7: Declares a nullable String variable named nullableString, initialized to null.

  • Line 8: Declares a nullable Int variable named nullableInt, initialized to null.

  • Line 10: Prints the string variable, outputting “Hello, Kotlin!”.

  • Line 11: Prints the array variable, outputting “1, 2, 3, 4, 5”.

  • Line 12: Prints the nullableString, outputting “null”.

  • Line 13: Prints the nullableInt, outputting “null”.

Conclusion

In conclusion, Kotlin offers a diverse range of data types catering to various programming needs. These data types can be broadly categorized into primitive types and reference types, each serving specific purposes within Kotlin applications.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved