Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
Types of Variables – Kotlin Tutorials for beginners
Types of Variables – Kotlin Tutorials for beginners

Types of Variables – Kotlin Tutorials for beginners

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

IntegerFloating pointBooleanCharacters 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

TypeByteShortIntLong
Memory allocation8 bits16 bits32 bits64 bits
Range-128 to 127-32768 to 32767-2,147,483,648 to 2,147,483,647very large values
Examplevar x : Byte = 97var x : Short = 10000var x = 6var x = 6L
Operations performedconversion to other types, mathematical operationconversion to other types, mathematical operationconversion to other types, mathematical operationconversion 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

TypeFloatDouble
Memory allocation32 bits64 bits
Range-2,147,483,648 to 2,147,483,647very large values
Examplevar x = 123.5var x = 123.5F
Operations performedconversion to other types, mathematical operationconversion 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”

TypeBoolean
Memory allocation1 bit
Rangetrue or false
Examplevar isBoolean = true, var isBoolean = false
Operations performedlogical operations
  • logical operations such as AND ( && ), OR ( || ) and nagation (!)

4. Characters and Strings

TypeCharacters
Memory allocation 8 bits
Range-128 to 127
Examplevar 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

TypeBinary NumbersHexadecimal NumbersOctal Numbers
Declarationwith prefix 0bwith prefixing 0xNot supported
Examplevar x = 0b10var 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 .