SyntaxIntroduction to ScalaRunning Scala

Running Scala

Like Python, Scala can be executed interactively, directly from the command line, or by writing a Scala script using a text editor. To execute Scala interactively, just call scala from the command line. You can then type commands interactively, and they will be executed immediately:

Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_75).
Type in expressions to have them evaluated.
Type :help for more information.
scala> println("Hello World")
Hello World
scala> println("This is fun")  
This is fun
This is a good way to get to know a new programming language and to experiment with different types of data.

In most cases, however, you will want to write a script, consisting of Scala functions and statements to be executed. For example, we can create a file for1.scala with the following contents:

def square(n : Int) {
  println(n + "\t: " + n * n)
}

for (i <- 1 to 10)
  square(i)
We can run this script from the command line with the command scala for1.scala:
> scala for1.scala 
1	: 1
2	: 4
3	: 9
4	: 16
5	: 25
6	: 36
7	: 49
8	: 64
9	: 81
10	: 100
> 

We will see later that there is a third way to execute Scala programs, namely to create one or more files that are compiled using the Scala compiler. The advantage is that such a compiled programs starts much faster.

SyntaxIntroduction to ScalaRunning Scala