Kotlin Data Types

In this tutorial you will learn about the Kotlin Data Types and its application with practical example.

Kotlin Data Types

Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose to allocate some memory space for that variable. Every variable have data type associated to it, data type for a variable defines –

  • The amount of memory space allocated for variables.
  • A data type specifies the possible values for variables.
  • The operations that can be performed on variables.

In Kotlin, everything is an object, which means we can call member function and properties on any variable. Kotlin has following built-in Data Types –

  • Numbers
  • Characters
  • Booleans
  • Arrays
  • String

Kotlin Numbers:- The Number data type is used to hold the numeric values. Kotlin supports following numerical data types –

Type Size Description
Byte 8 8 bit Signed Integer (two’s complement)
Short 16 16 bit Signed Integer (two’s complement)
int 32 32 bit Signed Integer (two’s complement)
long 64 64 bit Signed Integer (two’s complement). They can also be represented in octal and hexadecimal
float 32 It can hold single precision 32 bit floating point value
Double 64 It can hold double precision 64 bit floating point value

Kotlin Characters:- The character data type is used to hold the single character. In Kotlin, Characters are represented by the type char. Character types cannot be treated as numbers directly. Character are declared using single quotes.

Example:-

Kotlin Boolean:- The Boolean data type is used to represent the truth values, which can be either True or False. Booleans are commonly used in decision making statements.

Recommended:-  Kotlin Overview

Example:-

Kotlin Arrays:- The array is collection of homogeneous elements placed in contiguous memory locations that can be accessed individually by adding an index or subscript to a unique identifier. In Kotlin, Arrays can be created using library function arrayOf() and Array class. Array class has set(), get () function and size property as well as some other useful member functions.

Creating Array using library function arrayOf()

In order to create an array using library function arrayOf(), you need to pass the item values to it as following –

Example:-

The individual elements of an array can be accessed using their index values (array[index]). Array index are always starts from zero.

Creating Array using Array() constructor

In Kotlin, an array can also be created using Array constructor. Array constructor takes following two parameters –

  1. Size of the array
  2. Function that returns the initial value of each array element given its index

Example:-

Kotlin String:- A string variable is used to hold series or sequence of characters – letters, numbers, and special characters. Strings are immutable.

Example:-

Types of String

Escaped String:- Escape String may contain escape characters like ‘\n’, ‘\t’, ‘\b’ etc, and declared within double quote (” “) as following –

Recommended:-  Kotlin Flow Control

Example:-

Raw String:- Raw string can contain multiple lines of text and it does not contain any escape character. Raw String is declared within triple quote (“”” “””) as following –

Example:-

In this tutorial we have learn about the Kotlin Data Types and its application with practical example. I hope you will like this tutorial.