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

Scala is not an exception. After you have setup the environment (instruction 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 data types in Scala: Byte, Short, Int, Long, Float, Double, Char, Boolean and Unit. If you have a previous experience with Java, you might know that Java has 8 primitive data types – all of them are enumerated above (exclude Unit), except . Furthermore, in Scala any data type is an object, everything is object, even functions are objects. This distinguishes Scala from Java, it doesn’t has primitives.

Since all of data types are objects, you can use a wide range of useful methods defined for them. For example transformation from one type to another (Int to Double and vice versa) etc.

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 part of 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. Exactly due to this fact such variables are called mutable – their values can be changed.

Also you can use variables in such way:

//This is String variable
var word = "Mutability is not reliable"

//This is 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 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 has appropriate value types before a compilation. This circumstance prevents a lot of bugs on early stages of development by avoiding assignment of String values to Boolean variables an so on.

Scala values (immutable)

Opposite to var variables, with 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-val

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 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 basics of variable declaration in Scala. It’s hard to understand in which situation you should use val or var in the beginning of the learning. 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 work with Scala.

About The Author

Mathematician, programmer, wrestler, last action hero... Java / Scala architect, trainer, entrepreneur, author of this blog

Close