Code Examples for

Programming in Scala

Return to chapter index

2 First Steps in Scala

Sample run of chapter's interpreter examples

Step 1. Learn to use the Scala interpreter


$ scala Welcome to Scala version 2.7.2. Type in expressions to have them evaluated. Type :help for more information. scala>
scala> 1 + 2
res0: Int = 3
scala> res0 * 3 res1: Int = 9
scala> println("Hello, world!") Hello, world!

Step 2. Define some variables


scala> val msg = "Hello, world!" msg: java.lang.String = Hello, world!
scala> val msg2: java.lang.String = "Hello again, world!" msg2: java.lang.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!" <console>:5: error: reassignment to val msg = "Goodbye cruel world!" ^
scala> var greeting = "Hello, world!" greeting: java.lang.String = Hello, world!
scala> greeting = "Leave me alone, world!" greeting: java.lang.String = Leave me alone, world!
scala> val multiLine = \| "This is the next line." multiLine: java.lang.String = This is the next line.
scala> val oops = \| \| You typed two blank lines. Starting a new command. scala>

Step 3. Define some functions


scala> def max(x: Int, y: Int): Int = { | if (x > y) x | else y | } max: (Int,Int)Int
scala> def max2(x: Int, y: Int) = if (x > y) x else y max2: (Int,Int)Int
scala> max(3, 5) res6: Int = 5
scala> def greet() = println("Hello, world!") greet: ()Unit
scala> :quit $

Step 4. Write some Scala scripts


println("Hello, world, from a script!")
$ scala hello.scala
Hello, world, from a script!
// Say hello to the first argument println("Hello, "+ args(0) +"!")
$ scala helloarg.scala planet
Hello, planet!

Step 5. Loop with while; decide with if


var i = 0 while (i < args.length) { println(args(i)) i += 1 }
$ scala printargs.scala Scala is fun
Scala is fun
var i = 0 while (i < args.length) { if (i != 0) print(" ") print(args(i)) i += 1 } println()
$ scala echoargs.scala Scala is even more fun
Scala is even more fun
var i = 0; while (i < args.length) { if (i != 0) { print(" "); } print(args(i)); i += 1; } println();

Step 6. Iterate with foreach and for


args.foreach(arg => println(arg))
$ scala pa.scala Concise is nice
Concise is nice
args.foreach((arg: String) => println(arg))
args.foreach(println)
for (arg <- args) println(arg)
$ scala forargs.scala for arg in args
for arg in args

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

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

and:

http://booksites.artima.com/programming_in_scala

Copyright © 2007-2008 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.