When you start learning a new programming language you likely begin with an environment setup, unless perhaps if you are learning JavaScript, because in this case a browser would be enough. I bet that the next step after the environment setup are variables. You definitely need to start from variables, because it’s the most essential topic in the process of learning a new programming language!

Scala is not an exception. After you have setup the environment (here are instructions for Mac & Windows) you can continue with variables and values. Scala has two ways for storing of data:

  1. Mutable (var)
  2. 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: Byte, Short, Int, Long, Float, Double, Char, and 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 (Int to Double and vice versa).

Here is a very rough schema of Scala classes hierarchy:

Scala-class-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-error-type-mismatch

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:

Scala-reassignment-to-valUse 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.

Scala-lazy-val

Summary

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.