Specifying and understanding Variable data types in Kotlin

So far we have learnt that variables:

  1. do have specific type called data types and they can be either string, integer, double, boolean etc.
  2. will have an inferred data type if user does not specify data type then IDE will infer the data type based on the value assigned to variable at the time of declaration.

But user can specify the data type manually at the time of variable declaration instead of leaving it on IDE to infer.

1 String

We can specify the data type as string i.e. text type. This is done by simply appended after the variable following colon as following:

So syntax is simple. You have a var/val keyword followed by the name of the variable then colon then string type and then the value of the variable. In the example above we have specified that this variable is of the type String. Again, by string we mean “text” based data.

2 Numbers

In Kotlin for numbers alone we have different data types that are following:

  1. Int
  2. Byte
  3. Short
  4. Long
  5. Float
  6. Double

These data types for numbers are there for different reasons and for different uses. Most of the time we are fine with Int alone but specific cases require specific data types that we will learn over the course of these lessons. Here I will try to describe the concept behind each and how they are used and their significance until we see them in actual use during application coding.

2.1 Int [Integer]

If we want to declare a variable as integer then it will be done as:

As told before, giving every data specific type of data type keeps code clean and meaningful and less error prone. For example if we are trying to declare String but mention and Integer as value, IDE won’t let us do it and will warn us with a red line:

This way user will be able to fix it either by changing the value to string type or changing the data type whatever is appropriate.

2.2 Boolean

Variables declared with Boolean as their data type can have only two possible values i.e. 0 or 1 which in other words is simply False and True respectively. Remember however, that this 0 and 1 is not the same 0 and 1 declared as integer or Int, here 0 is representing False and 1 is representing True.

Restricting the variable to Boolean is important assures that output of variable is always either True or False.

Here is one example with a logical test and if we print this variable we will be given a response in boolean:

And here is the result if we run this code:

Booleans play important role many times in every single app when we have conditions to operate and execute and have to carry out logical tests. We will be discussing in details about conditions in coming lessons.

2.3 Byte

This data type is also related with numbers. Int is one type related with numbers we already discussed above.

Byte is technically a unit of information or simply saying “unit of size” of information. As some of you might already know that information size is measured in bits and bytes to kilobytes, megabytes, gigabytes, terabytes and so on.

A byte or data type of byte takes 1 byte of space in other words, byte data type is the length of numbers that takes no more than 1 byte of space in memory or disk. In Java environment by byte we mean numbers between -128 and 127 or 0 and 255.

Here is an example of variable with data type byte:

At the moment this doesn’t look much different from Int but its usage is different. For example, color codes are given in bytes. Ever thought why we have colors 0 to 255? The ones who have little knowledge of HTML might understand it but others should not worry. This will make much more sense when we will discuss about colors and their codes.

2.4 Short and Long

Short and Long are also numbers related data types and unlike Byte that are based on memory size, short and long are based on length of numbers.

A short data type can take numbers ranging from -32768 to 32767. This is a significantly larger number than Byte as you can see and thus will require more space.

However, Int data type can take much larger number than Short data type. Range is from – billions to + billions. But even still if you need larger than these numbers then we have Long.

Here is one example depicting that Int can’t take really large numbers:

However, if I change the data type from Int to Long then this error will be resolved:

Another way to specify Long data type is to append “L” the end of the value as following:

2.5 Floating point numbers

Skipping the technicalities, floating point numbers are simply decimal numbers. In Kotlin we have two major floating point number types i.e. float and double. Difference between float and double is basically in how much memory space is available for whole number and fractional portion of a number. Float has smaller memory space availability than double that is why float are considered less precise than double.

I will try to make it understandable with a mathematical example.

Floating point numbers is basically a number representation method. For example I can have a number like 2687962 and this is one way of writing it. I can write the same number as: 2.687962 x 106. Or it can be like this: 2.68 x 10

This is a floating point representation. In first case it was simply a number with lots of digits. In second cases, we have divided the number in base and exponent. Dividing the numbers in such way can help us manage memory space. Even in our example of numbers with exponents we have two variants, one with larger or should I say precise base and the second with shorter base or should I say less precise base.

As I mentioned earlier, the difference between two data types is about the size each take in memory. Float takes less and double takes more than float. How much more? Here is the size each number type takes from Kotlin docs itself:

Type Bit width
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8

One thing to remember is that for float type we have to specify small “f” after the number to confirm that we intended to use float type data as following:

This is needed as by default Kotlin infers double as the data type for decimal numbers. And again we can specify the data type as double by specifying the number as double as following:

3 Char

Char is a short for character. This data type can hold a single character. It can be anything a letter, a symbol, digit but it has to be just single. An important requirement is that value of this data type needs to be in single quote instead of double quote that are reserved for string data type. So variable can be declared as Char as following:

And if you intend to use certain symbol that can have a distinct function in Kotlin for example $ sign is used to represent variable in string (we will cover that in future) then in such case you can strip its role or escape its default role with backslash before it as following:

In Kotlin chars are not numbers which is a different case compared to Java.

Leave a Reply

avatar
  Subscribe  
Notify of