Data Types in Kotlin

In this tutorial, we will learn about available Data Types in Kotlin. Data Types are used to refer to the type and size of the value in a variable. The data types in Kotlin are divided into various categories and these categories are as follow:

  • Number
  • Character
  • Boolean
  • Array
  • String

Number:

This category contains all those data types which can store both normal and decimal number. There is a total of six data types in this category which are as follow:

Data Type Memory Size Range
Byte 8 bit -128 to 127
Short 16 bit -32768 to 32767
Int 32 bit -2,147,483,648 to 2,147,483,647
Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38
Double 64 bit 4.94065645841246544e-324 to 1.79769313486231570e+308

Example Program:

un main(args: Array<String>) {
   val b: Byte = 1
   val s: Short = 10
   val i: Int = 10000
   val l: Long = 100000000
   val f: Float = 100.00f
   val d: Double = 100.00
 
   println("Byte Value is "+b);
   println("Short Value is "+s);
   println("Int Value is "+i);
   println("Long Value is "+l);
   println("Float Value is "+f);
   println("Double  Value is "+d);
}

Characters:

In Kotlin Char data type is used to define character in variable. Char value should be defined inside single quotes ('a').

Data Type Memory Size Range
Char 4 bit -128 to 127
fun main(args: Array<String>) {
   val gender: Char    // defining variable 
   gender = 'M'        // Assigning value
   println("Gender = "+gender)
}

Boolean:

In Boolean data type we can store only two value which are true or false.

Data Types Memory Size Values
Boolean 1 bit true or false

Example Program:

fun main(args: Array<String>) {
   val flag: Boolean   // defining variable 
   flag = true         // Assinging value 
   println("Value is "+"$flag")
}

Array:

In very simple language Array is a collection of homogeneous data elements. Kotlin has Array class to define an array in program. We can us arrayOf() function and Array() constructor to create an Array.

Creating an array using arrayOf() function:

fun main() 
{ 
    // declaring an array using arrayOf() 
    val numbers = arrayOf(1, 2, 3, 4, 5) 
    for (i in 0..numbers.size-1) 
    { 
        println(numbers[i]) 
    } 
} 

Creating an array using Array Constructor:

The Array constructor takes two parameters:

  1. The size of the array.
  2. A function which accepts the index of a given element and returns the initial value of that element.
fun main() 
{ 
    val numbers = Array(5, { i -> i * 1 }) 
    for (i in 0..numbers.size-1) 
    { 
        println(numbers[i]) 
    } 
} 

String:

In Kotlin String is represented as collect of characters. String is immutable in Kotlin like Java. We have two kinds of strings available in Kotlin which are raw String and escaped String.

Raw String:

We describe raw strings using  triple quote (""" """). We can create multi-line strings using raw strings.

fun main(args: Array<String>) {
   var rawString :String  = """  Learn   
                               Kotlin On  
                                Owlbuddy"""
   println(rawString)   
}

Escaped String:

We describe Escaped strings using double quote (" "). There are various escaped characters available in Kotlin which we can use with Strings like '\n', '\t', '\b' etc.

fun main(args: Array<String>) {
   val escapedString : String  = "\nThis is escaped String!\n"
   println(escapedString) 
}

 

Spread the love
Scroll to Top