Kotlin Basic Types — Numbers

K

The first topic to begin exploring Kotlin are data types, in this language, they are called ‘Basic’ types, which is not exactly the same as what many developers refer to as ‘Primitives’, but this is a topic for another post. Kotlin mentions in the documentation five of these types:

  1. Numbers
  2. Characters
  3. Booleans
  4. Arrays
  5. Strings

Across this post, I will focus on giving a friendly explanation about the first type ‘Numbers‘, because, even experienced programmers (me included), sometimes get overwhelmed by the amount of information we find in the ‘official docs’.

Numbers

I don’t think I need to explain what this type refers to. We all learn about numbers since our early stages in life and use them on a daily basis. I’m sure you can look around and find a couple of examples: numbers on a clock, a receipt, some money on your desk, numbers are everywhere. As in real life, numbers in Kotlin can be of two types, either they are integers, or they have decimals.

1. Integers

Integers are numbers that help us count ‘entire’ things. How many cars are in a certain street, the number of hours on a day, lightbulbs in a house, students in a school. The question is, how are these called in Kotlin? Most of the time they will be ‘Int‘, which gets initialized at 32 bits. If the size of the number chosen is larger than that, then they will be ‘Long‘, which is 64 bits. There are other two types: ‘Short’ and ‘Byte’, but those are not common, and you will rarely use them.

Examples of ‘Int‘:

val cars = 5 ?????
val hours = 24 ⏰
val lightbulbs = 3 ???

Examples of ‘Long‘:

val students = 50000000000 ? // too many studentsval neuronalConnectionsInTheBrain = 1000000000000000 ? // 10^14 approxval ageOfTheUniverse = 13800000000 ? // Estimated age of the universe 1.38*10^10

Why would I want to use Long instead of Int?

An example is the easiest way to show this. Let’s say you want to multiply two big numbers like the ones mentioned above. In this case, we will use 100000 90000, if you input this in the terminal the result will be the following:

val result = 100000*90000
print(result) //You will get: 410065408

As you may already know 410065408 is definitely not the result of that product. The thing is, as the number is too big, and the default for Val with numbers is a 32-bit integer, there was not enough space for the product to happen and we got this random result. So the decision is simple, if you are using large numbers go for long, otherwise, you’re safe.

2. Floating-Point

This type is used for numbers with decimal places. We have two types: ‘Float‘ and ‘Double‘, the difference is how many decimal places they have. Floats have 32 bits, so they will be for numbers of up to 6 or 7 decimals, while Double will be for up to 15 or 16 decimals, above that amount of decimals the quantity will be rounded. All values will be defined as ‘Double‘ unless you specify you want a ‘Float’, this is done by adding an ‘f‘ to the end of the number.

Examples of ‘Float‘:

val costOfDonut = 2.52f ?
val mySize = 1.53f ??‍♀️
val distanceToSchool = 1.25f ??‍♂️

Examples of ‘Double‘:

val costOfDonut = 2.52 ?
val mySize = 1.53 ??‍♀️
val distanceToSchool = 1.25 ??‍♂️

Notice the difference between the two sets of floating-point examples? Yes! The only difference is whether I added the f at the end or not!

This is all the basic info you need to know on the first primitive types, of course, you can dive deeper in the topic (and I may in a future post), but let’s allow this to sink in and we can go on for our next topic ‘Characters‘.

If you liked the content on this post, please follow me, visit my webpage evanamargain.com or more specifically my blog. You can also buy me a coffee to support me!

Until next time!

Evana Margain Puig

If you need any services regarding mobile apps please come to my webpage, evisoft.mx or drop me a line contacto@evisoft.mx.

About the author

Evana Puig

Add Comment

By Evana Puig

Evana Puig

Get in touch

Mobile Developer expert in Android and iOS, with 10 years of experience. Visit me at evanapuig.com. Author, and topic master at raywenderlich.com