Scala Basic Tutorial - How To Declare Variables And Types
In this tutorial, we will go over declaring variables and learn about the data types that are supported in Scala.
1. Immutable variables
As we described in the Scala Introduction tutorial, immutability is a first class citizen in the Scala programming language. To define immutable variable, we use the keyword
val with the following syntax:
val <Name of our variable>: <Scala type> = <Some literal>
As an example, you can define an immutable variable named donutsToBuy of type Int and assign its value to 5.
val donutsToBuy: Int = 5
Now, if you try to change the value of donutsToBuy to say 10, the Scala compiler will not be happy :(
donutsToBuy = 10
If you then hover your mouse on the red lines in IntelliJ, you will see the message "Reassignment to val". The message tells us that once a value has been assigned, we can no longer change it!
Feel free to review the Scala Introduction where we discussed some of the benefits of having immutable variables.
2. Mutable variables
OK, fair enough that Scala is a functional programming language which favours the immutable pattern. But, what if you are in a loop and need to increment some variable. That's a perfectly legitimate use case and to declare a variable as mutable, we use the keyword
var as opposed to
var favoriteDonut: String = "Glazed Donut" favoriteDonut = "Vanilla Donut"
- The Scala compiler no longer complains that we are reassigning the favoriteDonut value from "Glazed Donut" to "Vanilla Donut".
- The type of favoriteDonut is defined as String using syntax
3. Lazy initialization
Sometimes you may wish to delay the initialization of some variable until at the point where it is consumed by your application. This is usually referred to as lazy initialization and we need to make use of the
lazy val donutService = "initialize some donut service"
- We've not specified the type of donutService! However, in this case, the Scala compiler knew that it should be of type String. This is called type inference which we will see in the upcoming tutorials.
4. Scala Supported Types
If you have used other programming languages such as Java or .NET, you would intuitively look for the supported types similar to Java's primitives or .NET's built-in types. However, Scala does not have built-in types! Instead, it was designed from the ground up to have a set of classes for representing its supporting types as shown below:
val donutsBought: Int = 5 val bigNumberOfDonuts: Long = 100000000L val smallNumberOfDonuts: Short = 1 val priceOfDonut: Double = 2.50 val donutPrice: Float = 2.50f val donutStoreName: String = "allaboutscala Donut Store" val donutByte: Byte = 0xa val donutFirstLetter: Char = 'D' val nothing: Unit = ()
- Scala also provides a type called Unit. For now, you can think of the Unit type similar to Java's or .NET's void keyword.
5. Declare a variable with no initialization
Sometimes you may not know the value of your variable immediately. You can only assign your variable's value at some later point in time during the execution of your application.
Let's assume that you need to declare a variable called leastFavoriteDonut of type String, but you won't initialize it just yet.
var leastFavoriteDonut: String = _ leastFavoriteDonut = "Plain Donut"
- We've use the wildcard operator _ when defining our variable.
- Somewhere later in our code base, we can then assign a String value for our variable which in our case was "Plain Donut" String value.
- Now, this may be controversial as you should strive to use the immutable pattern. As a matter of fact, when you code review some Scala application, you should always look out for code smell such as the use of var! There are better ways to avoid the use of var which we will see in upcoming tutorials.
This concludes our tutorial on Scala Basic Tutorial - How To Declare Variables and Types and I hope you've found it useful!
Don't forget to like and share this page :)
In this article, we went over the following:
- How to declare immutable variables in Scala
- How to declare mutable variables in Scala
- How to declare the various data types that Scala provides
- How to declare a variable with no initialization.
- If you come from an Object Oriented language, it may be a paradigm shift for using the immutable pattern via the val keyword. But, it will come with lots of benefits especially as you start getting more familiar with the functional programming side of Scala.
The source code is available on the allaboutscala GitHub repository.
In the next tutorial, I will show you how to use String interpolation in Scala.
Share this article on