In this chapter, you will learn how to use Scala as an industrial-strength pocket calculator, working interactively with numbers and arithmetic operations. Cay Horstmann introduces a number of important Scala concepts and idioms along the way. You will also learn how to browse the Scaladoc documentation at a beginner’s level.

In this chapter, you will learn how to use Scala as an industrial-strength pocket calculator, working interactively with numbers
and arithmetic operations. We introduce a number of important Scala concepts and idioms along the way. You will also learn
how to browse the Scaladoc documentation at a beginner’s level.

As you can see, the interpreter also displays the type of the result—in our examples, Int, Double, and java.lang.String.

You can call methods. Depending on how you launched the interpreter, you may be able to use tab completion for method names. Try typing res2.to and then hit the Tab key. If the interpreter offers choices such as

toCharArray toLowerCase toString toUpperCase

this means tab completion works. Type a U and hit the Tab key again. You now get a single completion:

res2.toUpperCase

Hit the Enter key, and the answer is displayed. (If you can’t use tab completion in your environment, you’ll have to type
the complete method name yourself.)

Also try hitting the ↑ and ↓ arrow keys. In most implementations, you will see the previously issued commands, and you can
edit them. Use the ←, →, and Del keys to change the last command to

res2.toLowerCase

As you can see, the Scala interpreter reads an expression, evaluates it, prints it, and reads the next expression. This is
called the read-eval-print loop, or REPL.

Technically speaking, the scala program is not an interpreter. Behind the scenes, your input is quickly compiled into bytecode, and the bytecode is executed by the Java virtual machine. For that reason, most Scala programmers prefer to call it “the REPL”.

TIP

The REPL is your friend. Instant feedback encourages experimenting, and you will feel good whenever something works.

It is a good idea to keep an editor window open at the same time, so you can copy and paste successful code snippets for later
use. Also, as you try more complex examples, you may want to compose them in the editor and then paste them into the REPL.