Code Examples for

Programming in Scala, Fifth Edition

Return to chapter index

2 First Steps in Scala

Sample run of chapter's interpreter examples

Step 1. Learn to use the Scala REPL


$ scala Starting Scala REPL... scala>
scala> 1 + 2
val res0: Int = 3
scala> res0 * 3 val res1: Int = 9
scala> println("Hello, world!") Hello, world!

Step 2. Define some variables


scala> val msg = "Hello, world!" val msg: String = Hello, world!
scala> val msg2: java.lang.String = "Hello again, world!" val msg2: String = Hello again, world!
scala> val msg3: String = "Hello yet again, world!" msg3: String = Hello yet again, world!
scala> println(msg) Hello, world!
scala> msg = "Goodbye cruel world!" 1 |msg = "Goodbye cruel world!" |^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |Reassignment to val msg
scala> var greeting = "Hello, world!" var greeting: String = Hello, world!
scala> greeting = "Leave me alone, world!" greeting: String = Leave me alone, world!
scala> val multiLine = \| "This is the next line." multiLine: String = This is the next line.
scala> val oops = \| \| You typed two blank lines. Starting a new command. scala>

Step 3. Define some functions


def max(x: Int, y: Int): Int = if x > y then x else y
def max(x: Int, y: Int) = if x > y then x else y
val bigger = max(3, 5) // 5
scala> def greet() = println("Hello, world!") def greet(): Unit
scala> :quit $

Step 4. Write some Scala scripts


// In file first-steps-in-scala/hello.scala @main def m() = println("Hello, world, from a script!")
$ scala hello.scala
Hello, world, from a script!
// In file first-steps-in-scala/helloarg.scala @main def m(args: String*) = // Say hello to the first argument println("Hello, " + args(0) + "!")
$ scala helloarg.scala planet
Hello, planet!

Step 5. Loop with while; decide with if


// In file first-steps-in-scala/printargs.scala @main def m(args: String*) = var i = 0 while i < args.length do println(args(i)) i += 1
$ scala printargs.scala Scala is fun
Scala is fun
// In file first-steps-in-scala/echoargs.scala @main def m(args: String*) = var i = 0 while i < args.length do if i != 0 then print(" ") print(args(i)) i += 1 println()
$ scala echoargs.scala Scala is even more fun
Scala is even more fun
// In file first-steps-in-scala/withsemis.scala @main def m(args: String*) = { var i = 0; while (i < args.length) { if (i != 0) { print(" "); } print(args(i)); i += 1; } println(); }

Step 6. Iterate with foreach and for-do


// In file first-steps-in-scala/pa.scala @main def m(args: String*) = args.foreach(arg => println(arg))
$ scala pa.scala Concise is nice
Concise is nice
// In file first-steps-in-scala/epa.scala @main def m(args: String*) = args.foreach((arg: String) => println(arg))
// In file first-steps-in-scala/foreach.scala @main def m(args: String*) = args.foreach(println)
// In file first-steps-in-scala/forargs.scala @main def m(args: String*) = for arg <- args do println(arg)
$ scala forargs.scala for arg in args
for arg in args

For more information about Programming in Scala, Fifth Edition (the "Stairway Book"), please visit:

http://www.artima.com/shop/programming_in_scala_5ed

and:

http://booksites.artima.com/programming_in_scala_5ed

Copyright © 2007-2020 Artima, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.