This beginners tutorial is on types of variables in Kotlin, values each of these types take and memory allocated for each type.
Types of variables in Kotlin are
Integer | Floating point | Boolean | Characters and Strings |
They differ in the
- Type of value they take
- Memory allocated when an object of a type is created
- And operations that can be performed
when you declare a variable, an object is created and memory is allocated to it, the size of memory allocated depends on the type of the variable. Check Variables in kotlin for more on Variables.
1.Integer
Integer again has four types
Type | Byte | Short | Int | Long |
Memory allocation | 8 bits | 16 bits | 32 bits | 64 bits |
Range | -128 to 127 | -32768 to 32767 | -2,147,483,648 to 2,147,483,647 | very large values |
Example | var x : Byte = 97 | var x : Short = 10000 | var x = 6 | var x = 6L |
Operations performed | conversion to other types, mathematical operation | conversion to other types, mathematical operation | conversion to other types, mathematical operation | conversion to other types, mathematical operation |
- var x = 97, can also mean variable of type Int, if you want it to be of type Byte, you will have to explicitly mention the keyword “Byte” .
- var x = 10000, can also mean variable of type Int, if you want it to be of type Short, you will have to explicitly mention the keyword “Short”.
- If integer assigned is too large, the compiler will consider it as Long internally
- var x = 6, can also mean variable of type Int, if you want it to be of type Long, you will have to explicitly mention it by adding “L” at the end.
- conversion to other types can be done with the help functions toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble(), toChar()
- mathematical operations such + , – , * , / , % can be performed
2. Floating points
Float has two types
Type | Float | Double |
Memory allocation | 32 bits | 64 bits |
Range | -2,147,483,648 to 2,147,483,647 | very large values |
Example | var x = 123.5 | var x = 123.5F |
Operations performed | conversion to other types, mathematical operation | conversion to other types, mathematical operation |
- when a variable is declare using a fraction number, the compiler thinks of it as Double by default.
- to explicitly mention the type as Float, add “F” or “f” at the end, if this number contains 6 or 7 decimal digit, it would be rounded.
- If the double value contians more than 6-7 digits, the value will be rounded
- conversion to other types can be done with the help functions toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble(), toChar()
- mathematical operations such + , – , * , / , % can be performed
3. Boolean
The boolean type takes two values “true” or “false”
Type | Boolean |
Memory allocation | 1 bit |
Range | true or false |
Example | var isBoolean = true, var isBoolean = false |
Operations performed | logical operations |
- logical operations such as AND ( && ), OR ( || ) and nagation (!)
4. Characters and Strings
Type | Characters |
Memory allocation | 8 bits |
Range | -128 to 127 |
Example | var alphabet = ‘A’ |
- String is used to hold multiple characters
- Example for string var greeting = “Hello”
- Strins are immutable – meaning the value assigned doesnot change
- Operations on string involve converting the string to uppercase/lowercase, concatination etc
Additional Info
Other number formats supported in Kotlin
Type | Binary Numbers | Hexadecimal Numbers | Octal Numbers |
Declaration | with prefix 0b | with prefixing 0x | Not supported |
Example | var x = 0b10 | var y = 0xAB | _ |
Do you have these questions on your mind?
Now that we are familiar with the types, we’ll see more about variables in our next post More on variables .