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