Martin odersky step 1. learn to use the scala interpreter
It’s time to write some Scala code. Before we start on the in-depth Scala
tutorial, we put in two chapters that will give you the big picture of Scala,
and most importantly, get you writing code. We encourage you to actually
try out all the code examples presented in this chapter and the next as you
go. The best way to start learning Scala is to program in it.
To run the examples in this chapter, you should have a standard Scala
installation. To get one, go to http://www.scala-lang.org/downloads
and follow the directions for your platform. You can also use a Scala plug-
in for Eclipse, IntelliJ, or NetBeans, but for the steps in this chapter, we’ll
assume you’re using the Scala distribution from scala-lang.org.
If you are a veteran programmer new to Scala, the next two chapters
should give you enough understanding to enable you to start writing useful
programs in Scala. If you are less experienced, some of the material may
seem a bit mysterious to you. But don’t worry. To get you up to speed
quickly, we had to leave out some details. Everything will be explained in a
less “ﬁre hose” fashion in later chapters. In addition, we inserted quite a few
footnotes in these next two chapters to point you to later sections of the book
where you’ll ﬁnd more detailed explanations.
Step 1. Learn to use the Scala interpreter
The easiest way to get started with Scala is by using the Scala interpreter, an
interactive “shell” for writing Scala expressions and programs. Simply type
an expression into the interpreter and it will evaluate the expression and print
the resulting value. The interactive shell for Scala is simply called scala.
You use it by typing scala at a command prompt:
Welcome to Scala version 2.8.1.
Type in expressions to have them evaluated.
Type :help for more information.
After you type an expression, such as 1 + 2, and hit enter:
scala> 1 + 2
The interpreter will print:
res0: Int = 3
This line includes:
• an automatically generated or user-deﬁned name to refer to the com-
puted value (res0, which means result 0),
• a colon (:), followed by the type of the expression (Int),
• an equals sign (=),
• the value resulting from evaluating the expression (3).
The type Int names the class Int in the package scala. Packages in
Scala are similar to packages in Java: they partition the global namespace
and provide a mechanism for information hiding.
Values of class Int correspond to Java’s int values. More generally, all of Java’s primitive types have
corresponding classes in the scala package. For example, scala.Boolean
corresponds to Java’s boolean. scala.Float corresponds to Java’s float.
And when you compile your Scala code to Java bytecodes, the Scala com-
piler will use Java’s primitive types where possible to give you the perfor-
mance beneﬁts of the primitive types.
The resX identiﬁer may be used in later lines. For instance, since res0
was set to 3 previously, res0 * 3 will be 9:
scala> res0 * 3
res1: Int = 9
To print the necessary, but not sufﬁcient, Hello, world! greeting, type:
scala> println("Hello, world!")
The println function prints the passed string to the standard output, similar
to System.out.println in Java.