Kotlin Programming – Read-Only and Mutable Variables

In this tutorial you’ll learn the basics of programming in Kotlin. To follow along, you can make use of the Kotlin – Playground.

Let’s dive into code practice whilst learning about Kotlin Programming. Whenever you see codes, try it in the Kotlin – Playground. You may output anything you want in the console by using:

println("Hello, world!")

Read-Only and Mutable Variables

You use read-only and mutable variables to associate a name (such as firstName) with a value of a particular type (such as the String “Zakhia”). The value of a read-only variable cannot be changed once set while the value of a mutable variable can be changed later.

Declare read-only and mutable variables with the keywords val and var respectively. It is recommended to follow the camelcase convention of writing the names of read-only and mutable variables, with the initial letter in lowercase. For example, let’s say you want to declare a read-only variable with the name firstName and value Zakhia:

val firstName = "Zakhia"

You may output the value of the read-only variable firstName in the Kotlin – Playground by using:

println(firstName)

Now let’s say you want to declare a mutable variable with the name dayOfMonth and initial value Sunday:

var dayOfMonth = "Sunday"

You can provide a type when declaring a read-only or mutable variable by being specific of the kind of value it stores. Note that you need to specify the type of a variable when you are declaring it, if you are not going to initialize it immediately. Here is an example of a declared mutable variable of type String:

var programmingLanguage: String

Now we can set a value to the declared variable programmingLanguage:

programmingLanguage = "Kotlin"

String Templates

You already know how to output values of a read-only or mutable variable with println(). To include some text with a read-only or mutable variable value as output, you can do so as follows:

print("I love programming with $programmingLanguage.")

// getting the number of characters
println("Length is ${programmingLanguage.length}")

Comments

Comments are descriptive texts you write to clarify the purpose of some code. They are ignored by the Kotlin compiler. You may write single-line or multi-line comments.

Single-line comments:

// Writing a comment on a single line.

Multi-line comments:

/*
Writing a comment on
multiple lines.
 */

Integers

Integers are whole numbers such as 19 and -22. The keyword Int can be used to denote an integer read-only or mutable variable. Let’s declare a read-only variable x with an integer value of 4:

val x = 4

Floats

Floats are fractional numbers such as 2.99, -23.15. Kotlin provides two types of floating-point number:

  1. Double represents a 64-bit floating-point number with a precision of at least 15-16 decimal digits.
  2. Float represents a 32-bit floating-point number with a precision of 6-7 decimal digits.

Booleans

Booleans can either be true or false, which is referred as logical and the keyword Boolean can be used to denote a boolean read-only or mutable variable.

val isRaining = false
val isSunny = true

Conditional Statements

Conditional statements, also known as if statements, can be used when some conditions must be met before executing some code. An example can be:

if (isSunny) {
   println("I will go out!")
} else {
   println("I won't go out!")
}

Nullable

You can use a nullable type when a null value is possible. Use of a nullable type means either “a value of the intended type is available” or “a null value”. Nullable types work for any type. An example of a nullable type can be, declaring a read-only variable numberInString of type String and trying to convert that String value to Int:

val numberInString = "19"
val convertToNumber = numberInString.toInt() // returns an Int? (nullable Int)

You can set a nullable variable to null:

var description: String? = "A nullable variable of String type with some value" // description contains a value 
description = null // description contains a null value

You can compare a nullable variable against null, using an if statement, to find out if it contains a value.

if (convertToNumber != null) {
   println("convertToNumber has an integer value")
}

Now that you know that convertToNumber has an integer value, you can get its value by putting an exclamation mark (!) after convertToNumber.

if (convertToNumber != null) {
   println("convertToNumber has integer value $convertToNumber!")
}

Always make sure that a nullable variable has a value before putting an exclamation mark (!) to get its value.

Reference

https://kotlinlang.org/docs/reference/basic-types.html