W3Schools Learner's Blog

W3Schools Programming knowledge summary website



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 –

Byte88 bit Signed Integer (two’s complement)
Short1616 bit Signed Integer (two’s complement)
int3232 bit Signed Integer (two’s complement)
long6464 bit Signed Integer (two’s complement). They can also be represented in octal and hexadecimal
float32It can hold single precision 32 bit floating point value
Double64It 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.


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.


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 –


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


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


Types of String

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


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 –


No comments:

Post a Comment

Note: only a member of this blog may post a comment.