Kotlin – Data Type

Kotlin is a statically typed language like Java. That is, the type of a variable is known during the compile time. For example,

val size: Int
val width = 15.20

Here, the compiler knows that size is of type Int, and width is of type Double before the compile time.The built-in types in Kotlin can be categorized as:

  1. Numbers
  2. Characters
  3. Booleans
  4. String
  5. Array

Number TypeNumbers in Kotlin are similar to Java. There are 6 built-in types representing number :

1. Byte :

fun main(args : Array<String>) {
    val range: Byte = 80
    println("$range")
    // The code below gives error. Why?
    // val range1: Byte = 200
}

Output :

80

2. Short :

The Short data type can have values from -32768 to 32767 (16-bit signed two’s complement integer).It is used instead of other integer data types to save memory if it’s certain that the value of the variable will be within [-32768, 32767]

fun main(args : Array<String>) {
    val temperature: Short = -11245
    println("$temperature")
}

Output :

-11245

3. Int :The Int data type can have values from -2^31 to 2^(31)-1 (32-bit signed two’s complement integer).

fun main(args : Array<String>) {
    val score: Int = 100000
    println("$score")
}

Output :

100000

If you assign an integer between –2^31 to 2^(31)-1 to a variable without explicitly specifying its type, the variable will be of Int type. For example,

fun main(args : Array<String>) {
   // score is of type Int
    val score = 10
    println("$score")
}

If you are using IntelliJ IDEA, you can place cursor inside the variable and press Ctrl + Shift + P to see its type.

4. Long :The Long data type can have values from -2^63 to 2^63-1 (64-bit signed two’s complement integer).

fun main(args : Array<String>) {
    val highestScore: Long = 9000
    println("$highestScore")
}

Output :

9000

If you assign an integer value greater than 231-1 or less than -231 to a variable (without explicitly specifying its type), the variable will be of Long type. For example,

val distance = 10000000000 // distance variable of type Long

Similarly, you can use capital letter L to specify that the variable is of type Long. For example,

val distance = 100L // distance value of type Long

5. DoubleThe Double type is a double-precision 64-bit floating point.

fun main(args : Array<String>) {
    // distance is of type Double
    val distance = 999.5
    println("$distance")
}

Output :

999.5

6. Float : The Float data type is a single-precision 32-bit floating point. Learn more about single precision and double precision floating point if you are interested.

fun main(args : Array<String>) {
    // distance is of type Float
    val distance = 19.5F
    println("$distance")
}

Output :

19.5

Notice that, we have used 19.5F instead of 19.5 in the above program. It is because 19.5 is a Double literal, and you cannot assign Double value to a variable of type Float.To tell compiler to treat 19.5 as Float, you need to use F at the end.If you are not sure what number value a variable will be assigned in the program, you can specify it as Number type. This allows you to assign both integer and floating-point value to the variable (one at a time). For example:

fun main(args : Array<String>) {
    var test: Number = 12.2
    println("$test")
    test = 12
    // Int smart cast from Number
    println("$test")
    test = 120L
    // Long smart cast from Number
    println("$test")
}

Output :

12.2
12
120

Character (Char) Data Type

Kotlin represents character using char. Character should be declared in a single quote like ‘c’. Please enter the following code in our coding ground and see how Kotlin interprets the character variable. Character variable cannot be declared like number variables. Kotlin variable can be declared in two ways – one using “var” and another using “val”.

fun main(args: Array<String>) {
   val letter: Char // defining a variable
   letter = 'A' // Assigning a value to it
   println("$letter")
}

Output :

A

Boolean

Boolean is very simple like other programming languages. We have only two values for Boolean – either true or false. In the following example, we will see how Kotlin interprets Boolean.

fun main(args: Array<String>) {
   val letter: Boolean // defining a variable
   letter = true // Assinging a value to it
   println("Your character value is "+"$letter")
}

Output :

Your character value is true

Strings

Strings are character arrays. Like Java, they are immutable in nature. We have two kinds of string available in Kotlin – one is called raw String and another is called escaped String. In the following example, we will make use of these strings.

1. Escaped String : Escape String is declared within double quote (” “) and may contain escape characters like ‘\n’, ‘\t’, ‘\b’ etc.

2. Raw String : Row String is declared within triple quote (“”” “””). It provides facility to declare String in new lines and contain multiple lines. Row String cannot contain any escape character.

fun main(args: Array<String>) {
   var rawString :String = "I am Raw String!"
   val escapedString : String = "I am escaped String!\n"

   println("Hello!"+escapedString)
   println("Hey!!"+rawString)
}

The above example of escaped String allows to provide extra line space after the first print statement. Following will be the output in the browser.

Hello!I am escaped String!
Hey!!I am Raw String!

Arrays

Arrays are a collection of homogeneous data. Like Java, Kotlin supports arrays of different data types. In the following example, we will make use of different arrays.

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].

val id = arrayOf(1,2,3,4,5)
val firstId = id[0]
val lasted = id[id.size-1]

Creating Array using Array() constructor :

Creating array using Array() constructor takes two arguments in Array() constructor :

1. First argument as a size of array, and

2. Second argument as the function, which is used to initialize and return the value of array element given its index.

val asc = Array(5, { i -> i * 2 }) //asc[0,2,4,6,8]  

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.