Kotlin fundamentals Part I: variables & constants

nichiren96

Roel Tombozafy

Posted on March 12, 2023

Kotlin fundamentals Part I: variables & constants

Variables are the building blocks to know when you learn a new programming language. In today's article we are going to talk about variables in Kotlin.

Declaring variables: val and var

To declare a variable in Kotlin we use the keyword val or var. If declared with val, a variable is immutable. It means that once set the value of that variable can't be changed throughout the execution of the program. On the other hand, if a variable is declared with the keyword var, its value can change during the execution of the program.

val age = 18 // value can't change
var name = "Lionel Messi"
Enter fullscreen mode Exit fullscreen mode

When using the val keyword to declare a variable, you must set the initial value for that variable.

Kotlin is a statically typed language which means that every variable should have a specific type. To specify a type when declaring a variable, you put a semicolon and the type after the variable name

val age: Int = 18
val salary: Double = 3500.5
var name: String = "David"
var isAdmin: Boolean = false
Enter fullscreen mode Exit fullscreen mode

Kotlin has built-in types like other programming language:

  • Int for integers
  • Double for floating numbers
  • Boolean for boolean value
  • String for text

Although, you are not forced to specify the variable type thanks to a mechanism called "type inference". It means that Kotlin can infer (or simply guess ) the type of the variable

val country = "Madagascar"
Enter fullscreen mode Exit fullscreen mode

In the example, Kotlin will assume the the variable country is a String due to the presence of the double quotes

Define a constant

To define a constant in Kotlin you need to define a dedicated class and a companion object inside the class

class Constants {
    companion object {
        const val NUMBER_OF_DAYS_IN_YEAR = 365
    }
}
Enter fullscreen mode Exit fullscreen mode

To use that constant in your code, you need to import it like shown in the example below

import Constants.Companion.NUMBER_OF_DAYS_IN_YEAR
fun main() {
    println("One year has $NUMBER_OF_DAYS_IN_YEAR")
}
Enter fullscreen mode Exit fullscreen mode

Working with String

To create a string you can call the String constructor and assign it to a variable

val myString = String()
Enter fullscreen mode Exit fullscreen mode

Or by assigning a value when creating the variable

val myString = "Hello Kotlin"
Enter fullscreen mode Exit fullscreen mode

To get the length of a string, use the length property

val myString = "Kotlin"
myString.length
Enter fullscreen mode Exit fullscreen mode

Many methods are available for string manipulation

val myString = "Hello World"
myString.uppercase() // "HELLO WORLD"
val position = myString.indexOf("W") // 7
myString.substring(position) // "World"
Enter fullscreen mode Exit fullscreen mode

Like in Java, you can build a string using the StringBuilder class

var builder = StringBuilder("Hello EveryOne ")
    builder.append("Kotlin is cool")
Enter fullscreen mode Exit fullscreen mode

It will create the string "Hello EveryOne. Kotlin is cool"

That's all for today. See you in my next article

💖 💪 🙅 🚩
nichiren96
Roel Tombozafy

Posted on March 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related