Kotlin Data Types Tutorial

In this section, we will learn what data types are and how to use them in Kotlin.

Note: we’re assuming you’re already familiar with the variables in Kotlin.

What is Data Type in Kotlin? And Why There Are Data Types in Kotlin?(Variables Type)

Values in programming have different types and for this reason, we use the term data-type to differentiate the types of values used in a program.

For example, a value like 10 is considered as an Integer data type or Int for short. Or a number like 44.32 is called floating data type and we use the word Float to represent such type.

One of the places where we use data types, either explicitly or implicitly, is when creating a variable! Using a data type for a variable helps Kotlin to realize how much memory space should be set aside for that variable!

For example, if the data type of a variable is Int, that means the amount of memory space that Kotlin will set aside for that variable will be high enough so that we can store values ranged from 2147483648 to 2147483647.

The other reason of why we have data types in Kotlin is to categorize the set of behaviors that we can expect from a value!

For example, a value of type integer can involve in a mathematical operation (like multiplication for example) but you can’t expect a string value to be involved in such type of operation! Or we can expect a string value to have behaviors like allowing users to change the characters of the string value into uppercase or lowercase, for example!

So the data type helps Kotlin to specify the set of behaviors each value of the distinct data type can have.

Kotlin Data Types Categories

There are different types of data types in Kotlin, which in this section we will get to know them each:

Kotlin Byte

The Byte data type is used for when we want to store number values that are ranges from -128 to 127. This data type takes a small amount of memory and so it’s efficient for when the memory is at a premium.

Kotlin Byte Syntax:

var variableName: Byte

Kotlin Byte Size:

The memory size of a short variable is 1 byte.

Kotlin Byte Values

-128 to 128

Kotlin Byte Data Type Example:

fun main(){
    var age: Byte = 50 

    print(age)
}

Output:

50

Kotlin Short

The short data type is like Byte, with a little more memory space, and is used to store whole numbers ranged from -32768 to 32767.

Kotlin Short Syntax:

var variableName: Short

Kotlin Short Size:

The memory size of a short variable is 2 bytes.

Kotlin Short Values

The range of values we can set for a variable of type Short is limited to -32768 to 32767

Kotlin Short Data Type Example:

fun main(){
    var port: Short = 1200 

    print(port)
}

Output:

1200

Kotlin Int

The Int data type stands for integer and is used when we want to store a whole number that is ranged from -2147483648 to 2147483647.

Note: by default, if we don’t set the data type of a variable that is holding a whole number (like 10, 100 etc.) Kotlin will set the data-type of that variable to Int automatically.

Kotlin Int Syntax:

var variableName: Int

Kotlin Int Size:

The memory size of a variable of type Int is 4 bytes.

Kotlin Int Values

The value we put for a variable of type Int can be within the range of: -2147483648 to 2147483647

Kotlin Int Data Type Example:

var myNum: Int = 100000

println(myNum)

Kotlin Long

A Long data type is like Int but with higher memory space that allows it to hold higher range of whole numbers.

Kotlin Long Syntax:

var variableName: Long = valueL

Note that after the number we want to assign, there should be the letter L. See the example section.

Kotlin Long Size:

The memory size of a Long variable is 8 bytes.

Kotlin Long Values

The values we can set for a Long number is ranged from -9223372036854775808 to 9223372036854775808.

Kotlin Long Data Type Example:

var myNum: Long = 40000000L

println(myNum)

Kotlin Float

We use Float type when we want to store numbers with fraction into a variable! For example, 2.3 or 545.3325 etc.

Kotlin Float Syntax:

var myNum: Float = 4322.43F

Note that we put the letter F at the end of a value that wants to be stored in a Float type variable.

Kotlin Float Size:

A Float type variable is big enough to hold a value with 7 decimal digits.

Kotlin Float Data Type Example:

var myNum: Float = 32.33422F

println(myNum)

Kotlin Double

The Double data type works the same as a Float data type with the exception that it can hold decimal numbers of up to 15 decimal digits! So it has a higher precision compared to a Float type.

Note that by default, if you store a decimal number into a variable, the variable becomes of type Double.

Kotlin Double Syntax:

myNum: Double = 3.24322453453

Kotlin Double Size:

The size of a Double type variable is high enough that it can hold a number with 15 decimal digits.

Kotlin Double Data Type Example:

var myNum: Double = 42324.34634234

println(myNum)

Kotlin Boolean

The Boolean data type is mainly used for conditional operations like if-else, while loop etc.

There are only two values represented for this data type:

true and false

When we use the value true in a conditional operation like if statement, it means the operation is a success and the instructions within the body of the if statement should run. The value false means the opposite!

Don’t worry if this makes little sense at the moment! As we reach into the conditional statements section, we will explain the boolean values in more details.

Note that there are other types of operations that implicitly return either the value, true or false! For example, the logical and comparison operators always return a Boolean value as a result of involving in an operation.

Kotlin Boolean Syntax:

myBoolean: Boolean = true

myBoolean: Boolean = false

Kotlin Boolean Size:

The size of a Boolean value is one byte.

Kotlin Boolean Values

true

false

Kotlin Boolean Data Type Example:

myBoolean: Boolean = true

println(myBoolean)

Kotlin Characters (Char)

The Char data type is used to create a variable that can hold one single character! For example, if you want to have a variable that can store the character A in it, we can set the data type of that variable to Char and store this character in it.

Kotlin Char Syntax:

charVariable: Char = ‘A’

Note that in order to store a character into a variable, we need to surround that character using a pair of single quotation mark.

Kotlin Char Values

We can store any character (including numbers and symbols) into a variable of type Char as long as those characters are surrounded by a pair of single quotation mark.

Kotlin Char Data Type Example:

var myChar: Char = ‘A’

var secondChar: Char = ‘B’

var thirdChar = ‘&’

Note that you can’t store a single quotation symbol into a Char variable by default! This is because we use a single quotation mark to set the border of a character value! So using 3 single quotation marks (two for border and one as the value) confuses Kotlin on which ones define the borders and which one is the actual value!

But if you insist to do so, you can prefix that single quotation with a backslash \ symbol and store it.

For example:

var character: Char = ‘\’’

Kotlin String

A String data type is used to create variables that can store a string of characters as their values. For example, if you have a sentence like “this is a simple string of characters” you can create a variable of type String and store such value there.

Kotlin String Syntax:

var myStringVariable: String = “String value”

Note that a string value should be surrounded using a pair of double quotation.

Kotlin String Values

Any value can be stored into a variable of type String as long as it is surrounded using a pair of double quotation.

Note that by default you can’t store a double quotation inside a string value! This is because double quotation marks are used to set the borders of a string value! So using one in between makes Kotlin to think it is the closing border of the string value instead of just one character as part of the string value!

But if you insist on storing a double quotation as part of a string value into a variable, then that double quotation should be prefixed with the backslash \ symbol as:

“The name of the person is \” John Doe \” “

As you can see, the double quotation within the body of the pair is prefixed with the backslash symbol. This way, Kotlin knows that these double quotation marks within the body of the string value are part of the value itself and they should not be interpreted as something special other than just simple character!

Kotlin String Data Type Example:

var myString:String = “this is a string value”

var myString = “13 and 14 is equal to 27”

Kotlin Type Conversion (AKA Casting)

Note that when you have a variable of a specific type like Int, for example, it cannot be stored in another data type! If you do so, you’ll get error.

For example, you can’t store a value of type Int into a variable of type Double for example!

In short: the data type of a variable should exactly match the value it takes.

If you want to convert the data type of a value to something else, you need to explicitly ask Kotlin for it!

We will return to this topic in the Kotlin type conversion section.

Note: in programming languages like Java and C# we have a concept called implicit casting. Well, in Kotlin there’s no such thing as implicit casting!

Example: type casting in Kotlin

fun main(){
    var num1: Short = 100 
    var num2: Int = num1
    print(num2)
}

Output:

type mismatch: inferred type is Short but Int was expected

As you can see, we’ve tried to assign the value of a variable of type Short into a variable of type Int but we got an error as a result.

Again, in the Kotlin type casting section, we will return to this topic and show you how to solve this problem.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies