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 “fire 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 find 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:

$ scala
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-defined 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 benefits of the primitive types.
The resX identifier 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 sufficient, Hello, world! greeting, type:
Scala> println(“Hello, world!”)
Hello, world!
The println function prints the passed string to the standard output, similar
To System. out. println in Java.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)