Kotlin Data Type

In Java, we have two types of Data Type : primitive (e.g. int, long, boolean, byte, char, etc.) and reference types (e.g. array, String). Java uses wrappers (like java.lang.Integer) to make primitive types behave like objects. But in Kotlin, there is no such distinction. Instead, all types are objects.

  Kotlin built in data type are categorized as following different categories:

  1) Number
2) Character
3) Boolean
4) Array
5) String


Numbers

Number types of data are those which hold only number type data variables. It is further categorized into different Integer and Floating point.

The integer types available in Kotlin are:

Long=> 64 bit
Int=> 32 bit
Short=> 16 bit
Byte=> 8 bit

The floating-point types are:

Double => 64 bit
Float=> 32 bit

Char Data Type

Characters are represented using the keyword Char. Char types are declared using single quotes (' ').

Boolean Data Types

The Boolean type in Kotlin is the same as in Java. Its value can be either true or false. The operations disjunction (||), conjunction (&&), and negation (!) can be performed on boolean types, just like Java.

Array in Kotlin

Arrays in Kotlin are represented by the Array class. Arrays are created using library function arrayOf() and Array() constructor. Array has get (), set() function, size property as well as some other useful member functions.
In Kotlin, there are two main ways to create an array: using the helper function arrayOf() or the constructor Array() .

Creating Array using library function arrayOf()


fun main(args: Array<String>) {
    //creating array using arrayOf function
    var numbers = arrayOf(1, 2, 3, 4, 5, 6)

    //accessing single element
    println(numbers[0])
    println(numbers[1])
    println(numbers[4])

    
}
1
2
5

The arrayOf() function creates array of wrapper types. The item value are passed inside arrayOf() function like arrayOf(1,2,3) which creates an array[1,2,3]. The elements of array are accessed through their index values (array[index]). Array index are start from zero.

Creating Array using Array() constructor

fun main(args: Array<String>) {
    //creating array using Array() Constructor
    var numbers = Array(5, { i -> i+1 })

    println(numbers[0])
    println(numbers[1])
    println(numbers[2])
    println(numbers[3])


}
1
2
3
4

Creating array using Array() constructor takes two arguments in Array() constructor:
First argument as a size of array, and
Second argument as the function, which is used to initialize and return the value of array element given its index.

String

Strings in Kotlin can be created with either double quotes or triple quotes. In addition to that, escape characters can be used with double quotes.

val myString = "This is a String"
val escapeString = "This is a string with new line \n"

Kotlin also supports string interpolation or string templates. This is an easier way to build dynamic strings than concatenation, which is what we use in Java. Using string templates, we can insert variables and expressions into a string.

fun main(args: Array<String>) {
    //string example

    var a=40
    var b=50
    println("Sum of ${a} and ${b} is ${a+b}")


}
Sum of 40 and 50 is 90

In above code $ is used to put dynamic value inside string.