Welcome! Today, we're delving into Scala variables, critical elements in any programming language. They serve as containers that store and handle data. In this lesson, we'll explore what they are, learn about assigning values, naming them, and discuss the nature of constants in Scala.
A variable is a slot in memory that holds data; its content can change during a program's execution. For example, in the snippet below, the value "Alice"
held by myName
is replaced by "Bob"
:
Scala1@main def run: Unit = 2 var myName = "Alice" 3 println(myName) // Output: Alice 4 5 myName = "Bob" 6 println(myName) // Output: Bob
Scala features two types of variables - val
, which is immutable (cannot be changed once assigned), and var
, which is mutable (can be changed after its initial assignment).
As was mentioned earlier, in Scala, you can reassign values to variables declared with var
. However, variables declared with val
are immutable. Let's assign and reassign a value to myAge
to see this action:
Scala1@main def run: Unit = 2 var myAge = 25 3 println(myAge) // Outputs: 25 4 5 myAge = 30 6 println(myAge) // Outputs: 30
Now, let's attempt to alter a constant, myBirthYear
:
Scala1@main def run: Unit = 2 val myBirthYear = 1991 3 println(myBirthYear) // Outputs: 1991 4 5 myBirthYear = 1990 // Error: Val cannot be reassigned
In Scala, we adhere to the camelCase naming convention. CamelCase involves writing compound words or phrases such that each word or abbreviation begins with a capital letter, except for the first word. This practice helps in providing clear, purposeful names. Good examples include numberOfStudents
, accountBalance
, userDetails
. Poor examples like c
, xyz
, temp
lack a clear purpose and should therefore be avoided.
In Scala, val
is used for declaring constants as shown below:
Scala1@main def run: Unit = 2 val pi = 3.14159 3 println(pi) // Outputs: 3.14159 4 5 pi = 3.15 // Error: Val cannot be reassigned
Constants are useful for representing values that are not meant to change.
In Scala, you can incorporate variable values into a string using string interpolation. To do this, you need to write s
before the double quotes. This technique keeps your code concise and easy to read. Here's an example:
Scala1@main def run: Unit = 2 val name = "Scala learner" 3 val topicsCovered = 2 4 println(s"Hello, $name! You've covered $topicsCovered topics today") // prints "Hello, Scala learner! You've covered 2 topics today"
In the code snippet above, $name
and $topicsCovered
within the quotes act as placeholders. When the code is executed, Scala automatically substitutes them with the values of the name
and topicsCovered
variables.
Here's a simple program that incorporates the concepts we've learned:
Scala1@main def run: Unit = 2 var userName = "Alice" 3 val userAge = 25 4 5 println("User Details: ") 6 println(s"Name: $userName") // prints "Name: Alice" 7 println(s"Age: $userAge") // prints "Age: 25"
The primary purpose is to understand how variables can be used to store and manipulate data in Scala.
Congratulations! You've learned all about Scala variables, including their declaration, naming conventions, assignment operations, and constants. Next, we have exercises to help reinforce your new knowledge. Remember, practice is critical in mastering any concept, so tackle these exercises with enthusiasm! Here's to happy coding in Scala!