- Mutable (var)
- Immutable (val)
We will speak about each of them separately below, but at first, let’s consider common data types in Scala.
Scala Data Types
There are 9 of them:
Boolean. If you have previous experience with Java, you might know that Java has 8 primitive data types – all the same as are enumerated above. Furthermore, in Scala any data type is an object. Everything is an object; even functions are objects. This distinguishes Scala from Java, as Scala doesn’t have primitives.
Since all of the data types are objects, you can use a wide range of useful methods defined for them. For example, the transformation from one type to another (
Double and vice versa).
Here is a very rough schema of Scala classes hierarchy:
This picture should provide a high-level overview of Scala data types.
Scala Variables (Mutable)
Let’s start with a classic definition of variables, which are used in most programming languages.
var a: Int = 5 a = 25 var sum: Int = 10 + a //Use underscore to assign a default value var status: Boolean = _
That’s how you can use var for a variable declaration in Scala. The main feature of var is that you can reassign values to it, after the first declaration. Because of this, such variables are called mutable – their values can be changed.
Also, you can use variables in this way:
//This is a String variable var word = "Mutability is not reliable" //This is a Double variable var d = 2.5
As you noticed, we could declare variables without specifying any type for them, Scala will do it for us with the help of the type inference. Once you declare a variable of some type, Scala infers its type, and after that, you will be able to perform actions with the variable which its type implies.
One more important thing which you should keep in mind is that after the variable is declared, its type can’t be changed; otherwise you will see the error:
Scala has a reliable static type system. It checks that variables have appropriate value types before compilation. This circumstance prevents a lot of bugs during the early stages of development by avoiding assignment of
String values to
Boolean variables and so on.
Scala Values (Immutable)
Opposite to var variables, with the help of val you can declare values which can’t be reassigned. Once you declare a val, you will not be able to change its value in the future. So think about val like about a constant. That’s why they are called immutable.
val constantValue: Long = 1500 val myWords = "Learn as much as you can"
Pretty simple, isn’t it?
In case if you would try to assign any value to val after its declaration, Scala will show you an error:
Use immutability as much as you can, because it helps to build reactive systems. There is no need to worry that during an access to immutable value it would be changed.
Also, val can be lazy. Let’s assume you have a val which is calculated based on multiple another values which we don’t know yet or a process of its calculation could take a long time. These cases are good for lazy val.
Variables are the core of any programming language. In this post, you learned the basics of variable declaration in Scala. It’s hard to understand in which situation you should use val or var when first starting to learn Scala. Probably lazy values are useless for you now, because you can’t imagine any situation where it can be applied. But you are at the start of Scala learning, so be patient and continue to work with Scala.