Introduction
In Kotlin, an open-source programming language like Python, Javascript, etc. This language is commonly used for Android Development by developers.
Just like other programming languages, Kotlin also has Data Types for variables. The most basic Kotlin Data type is the primitive data type. In Java, we need to use wrappers (Java.lang.Integer ) for Primitive data types to behave like an Object. But in Kotlin, all the data types already act as objects.
This article will learn about different Kotlin data types and type conversion.
Kotlin Data Types
In this section, we will learn about Kotlin data types. Data types in Kotlin are divided into the following groups:
- Numbers
- Booleans
- Characters
- Strings
Numbers
We have two data types to manage numbers in Kotlin.
Integer Data types
Integer data types store whole, positive or negative numbers (such as 121 or -457) without decimals. We have four types with different sizes and value ranges in Integer data types.
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 |
Example usage of these data types can be seen below:
val two = 2 // Int
val twoBillion = 2000000000 // Long
val twoLong = 2L // Long
val twoByte: Byte = 2 // ByteFloating-point Data types
Floating-point Data types represent numbers with the fractional part containing one or more decimals values. We have two types with different sizes and value ranges in floating-point data types.
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 |
Example usage of these data types can be seen below:
val pi = 3.141 // Double
// val two: Double = 2 // Error: type mismatch
val oneDouble = 1.0 // Double
val e = 2.7182818284 // Double
val eFloat = 2.7182818284f // Float, actual value is 2.7182817
Note: If we don't specify the data type for any numerical variable, it is most often considered as Int type for whole numbers. Similarly, for floating-point numbers, Double is considered.
Boolean
The Boolean data type can only store the values of either true or false. It is a one-bit data type.
Data type |
Bits |
Values |
| boolean | 1 bit | true or false |
Example usage of this data types can be seen below:
val a: Boolean = true
val b: Boolean = false
println(a || b) // The Output will be true or 1.
println(a && b) // The output will be false or 0.Characters
The Character data type can only store a single character, i.e., alphabets from (a-z) and ( A - Z), digits from ( 0 - 9), other symbols, and special characters. The type char represents it.
Data type |
Bits |
Range |
| char | 8 bits | -127 to 128 |
Note: Special characters starts with the ‘\' ( escaping backspace ). For example, most common special characters are: ‘\t', ‘\n', ‘\b', etc.
Example usage of this data type can be seen below:
val anyChar: Char = 'a'
println(anyChar)
println('\n') // Prints an extra newline characterStrings
Strings Data types:- The String data type can store a sequence of characters. The string value is the sequence of characters enclosed in double quotes ("").
Example usage of string data types can be seen below:
val str = "Hello World"
println(c) // Prints Hello World
Strings literals:- Literals are the source code representation of any fixed values. For example, "Hey CodingNinjas!" is a string literal that will appear directly in a program without requiring any computation (like variables).
We have two types of string literals in Kotlin:
- escaped strings containing escaped characters (like \n, \t, \b).
- raw strings that contain arbitrary text and newlines. A raw string is delimited by a triple quote (""").
Example usage of string literals can be seen below:
// Example of escaped string
val str = "Hello, world!\n"
// Example of raw string
val text = """
for (c in "foo")
print(c)
"""




