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:
Kotlin has a rich set of built-in primitive data types that are used for storing simple values.
Data Type | Description | Range |
| 8-bit signed integer | -128 to 127 |
| 16-bit signed integer | -32768 to 32767 |
| 32-bit signed integer | -2^31 to 2^31 - 1 |
| 4-bit signed integer | 2^63 to 2^63 - 1 |
| 32-bit floating point number | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
| 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 typesval intNumber: Int = 42val doubleNumber: Double = 3.14println(intNumber)println(doubleNumber)}
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 typesval char: Char = 'A'println(char)}
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 typesval boolean: Boolean = trueprintln(boolean) // Use the variable to avoid the warning}
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.
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 typesval string: String = "Hello, Kotlin!"val array: Array<Int> = arrayOf(1, 2, 3, 4, 5)// Nullable typesval nullableString: String? = nullval nullableInt: Int? = nullprintln(string)println(array.joinToString())println(nullableString)println(nullableInt)}
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”.
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