Analytics Vidhya
Published in

Analytics Vidhya

Baby Steps to Programming #4 Variable Types in Kotlin

Photo by Shitota Yuri on Unsplash

Hi there,

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:

  • Numbers
  • Characters
  • Booleans
  • Arrays
  • Strings

Specifying Types

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…

Numbers

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.

Integer numbers
In the Kotlin programming world, we have different types of integers, they are shown in the table below:

Integer Number Types. Source: Kotlin Documentation pdf.

Fam, don’t let how the table looks like scare or bother you. One very important thing to note is the Min and 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.

Integer variables holding Max values based on their type.

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…

Assigning a larger value than the max of an Int to an Int variable type.
Numbers aren’t easy, go take a cup of water.

Floating Point Numbers (decimals)
There are two types here, just Float and Double as they are popularly called. Just as before, take a look at its table:

Floating point number types. Source: Kotlin Documentation pdf.

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. The Double holds more decimal digits than a Float. To indicate you want a Float append the decimal with a F or f

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.

Booleans

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.

Characters

The character type of a variable has values which are single values encapsulated in single quotes. It is represented by the Char keyword.

val aCharacterVariable : Char = 'c'
val
anotherCharacterVariable : Char= '1'
val
aCharacter = '+' // Char type is inferred.

println(aCharacter) // +
println(aCharacterVariable) // c
println(anotherCharacterVariable) // 1

Arrays

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[4])
println("The last fruit in the fruit array is " + fruitArray[2])

// COMPILATION OUTPUT
//
// The 5th element in the intArray is 5
// The last fruit in the fruit array is melon

Arrays start counting from 0, that’s why intArray[4] picks 5 and fruitArray[2] picks melon.

Strings

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!

Raw Strings
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 = """
what's up?
I'm good

You sure?
really Sure!

"""

… 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 ❤️

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store