Lesson 2

Understanding Kotlin Variables: From Basics to Constants

Introduction and Lesson Overview

Welcome! Today, we're delving into Kotlin variables, which are critical elements in any programming language. They function as containers that store and handle data. In this lesson, we will explore what they are, learn about assigning values and assigning names to them, and discuss what constants are in Kotlin.

What are Variables in Kotlin?

A variable is a slot in memory that stores data, its content can change during the execution of the program. For example, in the snippet below, the value "Alice" held by myName is replaced by "Bob":

Kotlin
1var myName = "Alice" 2println(myName) // Output: Alice 3 4myName = "Bob" 5println(myName) // Output: Bob

Kotlin has two types of variables - val, which is immutable, and var, which is mutable.

Declaring Variables in Kotlin

In Kotlin, you can reassign values to variables declared with var, but variables declared with val are immutable. Let's assign and reassign a value to myAge to see this in action:

Kotlin
1var myAge = 25 2println(myAge) // Outputs: 25 3 4myAge = 30 5println(myAge) // Outputs: 30

Now, let's try to alter a constant, myBirthYear:

Kotlin
1val myBirthYear = 1991 2println(myBirthYear) // Outputs: 1991 3 4myBirthYear = 1990 // Error: Val cannot be reassigned
Kotlin Variable Naming Conventions

In Kotlin, we adhere to the camelCase naming convention, which helps deliver clear, purposeful names. Good examples include numberOfStudents, accountBalance, userDetails. Poor examples like c, xyz, temp lack clear purpose and should, therefore, be avoided.

Understanding Constants in Kotlin

Kotlin uses const val for constants, which are declared at the top level:

Kotlin
1const val PI = 3.14159 2println(PI) // Outputs: 3.14159 3 4PI = 3.15 // Error: Val cannot be reassigned

Constants are used for values that are not subject to change.

Basic String Interpolation in Kotlin

In Kotlin, you can incorporate variable values into a string using a technique called string interpolation. This technique makes your code more concise and easy to read. Here's an example:

Kotlin
1fun main() { 2 val name = "Kotlin learner" 3 val topicsCovered = 2 4 println("Hello, $name! You've covered $topicsCovered topics today") // prints "Hello, Kotlin learner! You've covered 2 topics today" 5}

In the code snippet above, $name and $topicsCovered within the quotes act as placeholders. Kotlin automatically substitutes them with the values of the name and topicsCovered variables when the code executes.

Practical Application

Let's construct a simple program that applies the concepts we've learned:

Kotlin
1var userName = "Alice" 2val userAge = 25 3 4println("User Details: ") 5println("Name: $userName") // prints "Name: Alice" 6println("Age: $userAge") // prints "Age: 25"

The main takeaway here is the utility of variables in storing and manipulating data in Kotlin.

Lesson Summary and Practice

Congratulations! You've learned all about Kotlin variables, including their declaration, naming conventions, assignment operations, and constants. Next up, we have exercises to help reinforce your new knowledge. Remember, practice is key to mastering any concept, so dive in with enthusiasm! Happy coding in Kotlin!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.