Kotlin Basic Types

Here you will learn about kotlin basic types.

Data types are fundamental when it comes to any programming language, basically it tells the compiler the type of data it should expect in the defined variable. It can be a string, bool, integer, float, double, char and many more. Lets discuss them one by one in detail.

Kotlin Basic Types

Number

There are total 6 types that comes under number.

Type Size (Bytes)
Byte 1
Short 2
Int 4
Long 8
Float 4
Double 8

Some Examples to support the same:

val someInt = 553		//Integer
val someLong = 43L		//Long
val someFloat = 314.43F		//Float
val someDouble = 425.78		//Double
val someHexadecimal = 0x4F	//Hexadecimal
val someBinary = 01010101	//Binary (Base 2)
val someByte: Byte = 10		//Byte

In kotlin we don’t have to explicitly mention the type of a variable. Still if you want you can mention in following way.

val someInt: Int = 553

You can learn more about variables here.

Boolean

The Boolean type can have two values, either true or false.

val someTrueBoolean = true
val someFalseBoolean = false 

val a = 1
val b = 2
val find = a < b	// variable find will contain true

String

Strings in kotlin are as simple as it sounds.

val str = "This is some random string with new line\n"

It is must to surround it via double quotes or a multiline string can be declared as shown below:

val multipleStringLines = """
        It's the first line
        It's the second line
        It's the third line """

Char

The data type char, stores a single letter, number or a symbol. Char is conceptually a fixed length, blank padded string. Any trailing spaces is removed on input, and only restores on output. The default length is 1. The size of character variable is 1 byte.

val someChar: char = 'A'		//Defined by single quotes ('x')
val symbolChar = '@'
val numChar: char = 5'

Array

Arrays are defined by the arrayOf() operator:

val randomArray = arrayOf(4, 5, 6, 7)
val anotherArray = arrayOf(2, 3, "Mayank", true)

We can also define array that contain specific type of values using utility functions such as charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf() and this can be done for all primitive datatypes.

We will discuss more about array in our later tutorials.

Comment down below if you have any queries related to kotlin basic types.

Leave a Comment