Scala Lesson 4 – Variables | Dataplexa

Variables in Scala

In this lesson, you will learn how variables work in Scala. Variables are fundamental building blocks used to store data and control program behavior.

Scala treats variables differently from many traditional languages by encouraging immutability and safer coding practices. Understanding this concept early will help you write better Scala programs.


What Is a Variable?

A variable is a named location in memory that stores a value. In Scala, variables are created using two keywords:

  • val — immutable (cannot be changed)
  • var — mutable (can be changed)

Choosing the right type of variable is an important design decision in Scala.


Immutable Variables with val

A variable declared with val cannot be reassigned after it is created. This makes code safer and easier to reason about.

val language = "Scala"
val year = 2024

Once assigned, the value stored in a val cannot be changed. Trying to reassign it will cause a compilation error.

val x = 10
// x = 20   // ❌ This will cause an error

Using val helps prevent bugs caused by unexpected changes.


Mutable Variables with var

Variables declared using var can be reassigned. They are useful when the value needs to change during program execution.

var count = 1
count = 2
count = count + 1

Although var is allowed, Scala encourages using it only when necessary.


Why Scala Prefers val

Immutability provides several benefits:

  • Safer concurrent programs
  • Easier debugging
  • Predictable behavior

In professional Scala projects, most variables are declared using val.


Type Inference

Scala can automatically determine the data type of a variable. This feature is called type inference.

val age = 25
val name = "Dataplexa"
val isActive = true

Even though types are not written explicitly, Scala still knows their types.


Explicit Type Declaration

You can also specify the type explicitly if you want clarity or strict control.

val score: Int = 90
var price: Double = 19.99
val title: String = "Scala Basics"

Explicit typing is often used in public APIs and large codebases.


Reassigning Variables

Only variables declared with var can be reassigned.

var level = "Beginner"
level = "Intermediate"

Reassignment changes the value stored in memory.


Variables Inside Code Blocks

Variables declared inside a block are scoped to that block only.

{
  val x = 10
  println(x)
}

// println(x)  // ❌ Not accessible here

This helps keep programs organized and prevents accidental misuse of variables.


Best Practices for Variables

When writing Scala code, follow these best practices:

  • Prefer val over var
  • Use meaningful variable names
  • Limit variable scope
  • Avoid unnecessary reassignment

📝 Practice Exercises


Exercise 1

Create an immutable variable storing your name.

Exercise 2

Create a mutable variable and update its value.

Exercise 3

Declare a variable with an explicit type.


✅ Practice Answers


Answer 1

val myName = "Scala Learner"

Answer 2

var counter = 0
counter = counter + 1

Answer 3

val total: Int = 100

What’s Next?

In the next lesson, you will explore data types in Scala. Understanding data types will help you write precise and efficient programs.