Baby Steps to Programming #4 Variable Types in Kotlin
I trust the holidays have been great, its also my pleasure to wish you a Happy New Year! Welcome to the fourth lesson in the series on Baby Steps to programming in Kotlin. Today we would be looking at Variable Types. It should be an interesting and enlightening read. Let’s get to it.
From the previous article, having understood what variables are, you should have noticed that those variables can store different things like numbers and string texts. This article focuses on telling you more about what type of stuff a variable can reference. Before we jump into the real work, let’s keep in mind the basic variable types which are:
Variables in Kotlin always have types. This means that variables are always of a type; be it a string, a number or whatever. The type of a variable can be explicitly written or the kotlin compiler infers what type the variable is when creating a variable. Take a look at how that is written below for both cases:
* EXPLICIT DECLARATION OF VARIABLE TYPE SYNTAX
* val variableName : variableType = variableValue
* DECLARATION OF VARIABLE TYPE WITHOUT SPECIFYING TYPE
* val variableName = variableValue
* N.B. compiler infers what type variable is.
I hope this did make sense 🤷🏼♂️, even if it didn’t totally, I guess you’ll get it as we progress. Now to the actual variable types…
From our primary school days everyone knows what numbers are, whether you liked it or not, Lol, you sure know what they are. Well, just like in maths we have whole numbers and decimal numbers. Correspondingly, in Kotlin programming we have integer numbers and floating point numbers.
In the Kotlin programming world, we have different types of integers, they are shown in the table below:
Fam, don’t let how the table looks like scare or bother you. One very important thing to note is the
Max values of the Integer types. It simply implies the maximum and minimum number values you can assign to a variable of a certain type. I hope this makes sense.
According to the previous table, try assigning something bigger than the
max value to a certain variable type and see what happens.
The compiler complains…
Floating Point Numbers (decimals)
There are two types here, just
Double as they are popularly called. Just as before, take a look at its table:
Our important takeout here is the decimal digits that differentiates the types. lets take an example:
/* FLOAT AND DOUBLE NUMBER TYPE
val floatVariable : Float = 3.123456789f
val doubleVariable : Double = 3.123456789
println(floatVariable) // 3.1234567 rounded up to fit in a float
println(doubleVariable) // 3.123456789
When you write a decimal number in Kotlin, it defaults to a
Double holds more decimal digits than a
Float. To indicate you want a
Float append the decimal with a
Try to play with number variables and types in your IDE on your own, remember to leave a comment or note for any misunderstandings or thoughts.
The computer has its own way of saying Yes or No, though not literally, it uses true or false. This is the boolean type. A variable is of boolean type when it holds either a true or a false.
/* BOOLEAN TYPE
val isEarthSpherical : Boolean = true
val isEarthFlat : Boolean = false
val someBoolean = true
Explicitly specifying the type can be omitted, the compiler can infer it for you.
The character type of a variable has values which are single values encapsulated in single quotes. It is represented by the
val aCharacterVariable : Char = 'c'
val anotherCharacterVariable : Char= '1'
val aCharacter = '+' // Char type is inferred.
println(aCharacter) // +
println(aCharacterVariable) // c
println(anotherCharacterVariable) // 1
When you want to assign a set of values to a single variable, arrays come in handy. They can hold multiple values. The
Array type is represented by
Array<T>. Where T is the type of values the array contains. An example is shown below:
* ARRAY TYPE
val intArray : Array<Int> = arrayOf(1, 2, 3, 4, 5)
val booleanArray : Array<Boolean> = arrayOf(true, false, false)
val fruitArray : Array<String> = arrayOf("apple", "oranges", "melon")
Accessing array entries
Getting the values in the
Array is quite as easy, you use
[ ] square brackets. The index of the value you want going inside the square brackets.
println("The 5th element in the intArray is " + intArray)
println("The last fruit in the fruit array is " + fruitArray)
// COMPILATION OUTPUT
// The 5th element in the intArray is 5
// The last fruit in the fruit array is melon
Arraysstart counting from 0, that’s why
intArraypicks 5 and
They are texts that go in quotes or triple quotes. You should’ve been seeing them since the dawn of time. An example is shown below:
val aString : String = "what's on your mind? "
println(aString) // what's on your mind?println(aString + "None of your business!")
// what's on your mind? None of your business!
This is a cool feature. These are strings that are wrapped in triple quotes. The strings print out exactly as they are typed, retaining their formatting. It can also contain arbitrary texts.
val rawString = """
… and this is the output:
… and Voila!! You’ve finally reached the end 🤗🤗
Congratulations!! you made it! Thank you so much for taking out time to read this article. Stay tuned for more lessons.
The entire source code for all lessons can be found at this repo: https://github.com/simi-aluko/baby-steps-to-programming-in-kotlin
Blessed up ❤️