Kotlin Variables & DataTypes

 
This article shows you how to declare variables in kotlin language. We are also going to learn how many datatypes supported by kotlin and how to use them for creating variables.

 

Variables

A variable is referred to a location to store data in memory in any programming language. The type of variables defines the range of value that the variable can hold. To indicate a storage area, each variable should be given a unique name.  

 

How to declare variables in Kotlin

There are two types of variables in Kotlin  

  1. Immutable Variable
  2. Mutable Variable
 
 

Immutable

Immutable variable means you can not change value of this variable after once it is declared. val keyword is used for this variable. You can also say that immutable variable means read only variable. It can not be reassigned after it is initialized.

You can declare it as below

val str = “hello”
str = “hi” // Error: Val cannot be reassigned
 
 

Mutable

Mutable variable means you can change variable value whenever you want. To declare this variable var keyword is used. This variable can be reassigned after it is initialized. You can declare it as below

var str = “hello”
str = “hi” // No Error


You can change value of variable but not its type. For example

var str = “hello”
str = 10 // Error
 
 

Difference with Java

You can declare multiple variables on single line in java. But in kotlin this is not possible. You can declare only one variable on single line. 

val str = “hello”, str1 = “hi” // Error
val str = “hello” // No Error
val str1 = “hi” // No Error
 
 

DataTypes

  There are five data types in kotlin

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

Number

Byte (8 bit) : This data type has values from -128 to 127.

val myByte: Byte = 73
 

Short (16 bit) : This data type has values from -32768 to 32767.

val myShort: Short = -259
 

Int (32 bit) : This data type has values from -2,147,483,648 to 2,147,483,647.

val myInt = 200
 

Long (64 bit) : This data type has values from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. We have to add suffix L for this data type.

val myLong = 5000L
 

Float (32 bit) : This data type has values from 1.40129846432481707e-45 to 3.40282346638528860e+38. We have to add suffix L for this data type.

val myFloat = 189.22F
 

Double (64 bit) : This data type has values from 4.94065645841246544e-324 to 1.79769313486231570e+308.

val myDouble = 137.82
 

Character

This data type is represented using char keyword. It is declared using single quote.

var digit = ’28’
 

Boolean

Value of Boolean type can be true or false. It is used to represent logical values.

val myBoolean = true
 

Array

Arrays in Kotlin are represented by the Array class. You can create an array in Kotlin either using the library function arrayOf() or using the Array() constructor. Array has get (), set() function, size property as well as some other useful member functions.

var numbers = arrayOf(“sun”,”mon”,”tue”,”wed”,”thr”,”fri”,”sat”)
 
val generatedStringArray = Array(10) { i -> “Number of index: $i”}
 

String

Strings can be created with either double quotes or triple quotes. 

var str= “Hello”
 
val escapeString = “This is a string with new line n”
 
val multipleStrLines = “””
first line
second line
third line “””
 

  You can access the character at a particular index in a String

var str = “hello”
var firstChar= name[0]
 

You can also show dynamic string using $ character.

val str = “Hello”
val letter = “The first letter is ${str.first()}”
 
 

Leave a Reply