Home
Scala
var and val Examples
This page was last reviewed on Dec 14, 2023.
Dot Net Perls
Var, val. We use var in Scala 3.3 to store a variable—it can be changed. A var can be reassigned. The val keyword, meanwhile, refers to a constant.
Program quality. Imagine a variable X is needed in 2 places. With val, an incorrect reassignment would not compile—we can correct it faster.
Var example. Let us use var in a simple Scala program. We bind the variable with identifier "animal" to the string literal "cat." Then were assign it (rebind it) to the string "frog."
Tip With this var, we can see the animal means different strings (cat and frog) at different points in time.
object Program { def main(args: Array[String]): Unit = { // Assign variable to a string literal. var animal = "cat" println(animal) // Reassign the variable. animal = "frog" println(animal) } }
cat frog
Val example. This program uses val for constants. When we use val, we cannot reassign the values—we must instead use separate bindings.
object Program { def main(args: Array[String]): Unit = { // Use val for a value that cannot be reassigned. val color1 = "magenta" println(color1) // Use a separate val. val color2 = "aqua" println(color2) } }
magenta aqua
Val error. What happens when we try to reassign a val? We get a compile-time error. We cannot change a val to point to a different value—it can only be set once.
error: reassignment to val animal = "frog" ^ one error found
Val type. With val we can specify a type. Here we specify two numbers with initial values of 10. But we specify size2 to be a Double, not an Int. It is printed with a decimal place.
object Program { def main(args: Array[String]): Unit = { // No type is specified, so Int is used. val size1 = 10 // Specify a Double type. val size2: Double = 10 // Print numbers. println(size1) println(size2) } }
10 10.0
Class fields. With var and val we specify the fields of a class. In Scala these are public. Fields can be accessed outside the class body by default.
Start We use var for width and height, as these can be mutated on Box instances. We assign them to 10 and 20.
Also The "name" is a constant value. It is a String. We cannot assign it, but we can access it and print its value.
String
println
class Box { var width: Int = 0 var height: Int = 0 final val name: String = "Box" } object Program { def main(args: Array[String]): Unit = { // Create a new Box instance. val box = new Box // Assign width and height var fields. box.width = 10 box.height = 20 // Print fields of box instance. println(box.width) println(box.height) println(box.name) } }
10 20 Box
Immutable things are important in Scala. We have List, Map, Set: these are immutable data structures. With val, we have constants, immutable values—these improve program quality.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 14, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.