Martin odersky step 2

Step 2. Define some variables

Scala has two kinds of variables, vals and vars. A val is similar to a final
Variable in Java. Once initialized, a val can never be reassigned. A var, by
Contrast, is similar to a non-final variable in Java. A var can be reassigned
Throughout its lifetime. Here’s a val definition:
scala> val msg = “Hello, world!”
msg: java. lang. String = Hello, world!
This statement introduces msg as a name for the string “Hello, world!”.
The type of msg is java. lang. String, because Scala strings are imple-
Mented by Java’s String class.
If you’re used to declaring variables in Java, you’ll notice one striking
Difference here: neither java. lang. String nor String appear anywhere
In the val definition. This example illustrates type inference, Scala’s ability
To figure out types you leave off. In this case, because you initialized msg
With a string literal, Scala inferred the type of msg to be String. When the
Scala interpreter (or compiler) can infer types, it is often best to let it do
So rather than fill the code with unnecessary, explicit type annotations. You
Can, however, specify a type explicitly if you wish, and sometimes you prob-
Ably should. An explicit type annotation can both ensure the Scala compiler
Infers the type you intend, as well as serve as useful documentation for fu-
Ture readers of the code. In contrast to Java, where you specify a variable’s
Type before its name, in Scala you specify a variable’s type after its name,
Separated by a colon. For example:

Scala> val msg2: java. lang. String = “Hello again, world!”
Msg2: java. lang. String = Hello again, world!

Or, since java. lang types are visible with their simple names4 in Scala
Programs, simply:
Scala> val msg3: String =

“Hello yet again, world!”
Msg3: String = Hello yet again, world!
Going back to the original msg, now that it is defined, you can use it as you’d
Expect, for example:
Scala> println(msg)
Hello, world!
What you can’t do with msg, given that it is a val, not a var, is reassign
5 For example, see how the interpreter complains when you attempt the
Scala> msg = “Goodbye cruel world!”
:6: error: reassignment to val
Msg = “Goodbye cruel world!”
If reassignment is what you want, you’ll need to use a var, as in:
Scala> var greeting = “Hello, world!”
Greeting: java. lang. String = Hello, world!
Since greeting is a var not a val, you can reassign it later. If you are
Feeling grouchy later, for example, you could change your greeting to:
Scala> greeting = “Leave me alone, world!”
Greeting: java. lang. String = Leave me alone, world!
To enter something into the interpreter that spans multiple lines, just keep
Typing after the first line. If the code you typed so far is not complete, the
Interpreter will respond with a vertical bar on the next line.

Scala> val multiLine =
| “This is the next line.”
MultiLine: java. lang. String = This is the next line.
If you realize you have typed something wrong, but the interpreter is still
Waiting for more input, you can escape by pressing enter twice:
Scala> val oops =
You typed two blank lines. Starting a new command.
In the rest of the book, we’ll leave out the vertical bars to make the code
Easier to read (and easier to copy and paste from the PDF eBook into the

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

Martin odersky step 2