Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we will use the scala REPL for an up and running example of Scala.
-
0:00
There are two ways of creating variables in Scala.
-
0:03
The preferred way is by creating a constant using the key word val.
-
0:07
This way of creating a variable supports the second idea of functional programming,
-
0:12
as the contents of a val cannot be changed.
-
0:15
Additionally, you can create a mutable variable in Scala
-
0:19
by using the key word var whose contents can be changed.
-
0:23
Let's see how this works in the REPL, the interactive shell, which we can get to by
-
0:27
opening up a terminal in a Mac or command prompt in Windows and typing Scala.
-
0:33
Let's open up the terminal to start the Scala REPL.
-
0:37
REPL stands for read, evaluate, print, and loop, and
-
0:41
it's very useful in evaluating expressions.
-
0:53
Here, we've created a variable name greeting.
-
0:55
A constant whose values cannot be changed.
-
0:58
Attempting to reassign the value of variable greeting
-
1:02
will throw a reassignment to val error.
-
1:05
Let's take a look at that.
-
1:10
Let's take a similar approach, but with a mutable variable this time.
-
1:19
We've created a variable called message, which contains the string, hello world.
-
1:24
Because vars are mutable, we can reassign its value.
-
1:33
In both instances, you may have noticed two things.
-
1:37
First, we aren't required to provide a semicolon at the end of the statement, and
-
1:41
that's because Scala supports semicolon inference.
-
1:45
Second, we didn't have to specify the parameter type.
-
1:49
That's because scala has a built in type inference mechanism
-
1:53
which allows it to figure out what the type of variable should be.
-
1:57
Although not recommended, we can always supply the semicolon or
-
2:01
the type information if we really wanted to.
-
2:04
Let's take a look.
-
2:12
Awesome, we learn how to create values and variables in Scala.
-
2:16
In the next video, we will create our first Scala app and learn about functions.
-
2:21
See you there!
You need to sign up for Treehouse in order to download course files.
Sign up