Getting started with Scala Language

Hello World by extending App

object HelloWorld extends App {
  println("Hello, world!")

Live demo

By extending the App trait, you can avoid defining an explicit main method. The entire body of the HelloWorld object is treated as “the main method”.

Delayed Initialization

Per the official documentation, App makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.

Delayed Initialization

Per the official documentation, App makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.

DelayedInit is now deprecated for general use, but is still supported for App as a special case. Support will continue until a replacement feature is decided upon and implemented.

To access command-line arguments when extending App, use this.args:

object HelloWorld extends App {
  println("Hello World!")
  for {
    arg <- this.args
  } println(s"Arg=$arg")

When using App, the body of the object will be executed as the main method, there is no need to override main.

Hello World by Defining a ‘main’ Method

Place this code in a file named HelloWorld.scala:

object Hello {
  def main(args: Array[String]): Unit = {
    println("Hello World!")

Live demo

To compile it to bytecode that is executable by the JVM:

$ scalac HelloWorld.scala

To run it:

$ scala Hello

When the Scala runtime loads the program, it looks for an object named Hello with a main method. The main method is the program entry point and is executed.

Note that, unlike Java, Scala has no requirement of naming objects or classes after the file they’re in. Instead, the parameter Hello passed in the command scala Hello refers to the object to look for that contains the main method to be executed. It is perfectly possible to have multiple objects with main methods in the same .scala file.

The args array will contain the command-line arguments given to the program, if any. For instance, we can modify the program like this:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello World!")
    for {
      arg <- args
    } println(s"Arg=$arg")

Compile it:

$ scalac HelloWorld.scala

And then execute it:

$ scala HelloWorld 1 2 3
Hello World!

Hello World as a script

Scala can be used as a scripting language. To demonstrate, create HelloWorld.scala with the following content:


Execute it with the command-line interpreter (the $ is the command line prompt):

$ scala HelloWorld.scala

If you omit .scala (such as if you simply typed scala HelloWorld) the runner will look for a compiled .class file with bytecode instead of compiling and then executing the script.

Note: If scala is used as a scripting language no package can be defined.

In operating systems utilizing bash or similar shell terminals, Scala scripts can be executed using a ‘shell preamble’. Create a file named and place the following as its content:

exec scala "$0" "[email protected]"

The parts between #! and !# is the ‘shell preamble’, and is interpreted as a bash script. The rest is Scala.

Once you have saved the above file, you must grant it ’executable’ permissions. In the shell you can do this:

$ chmod a+x

(Note that this gives permission to everyone: read about chmod to learn how to set it for more specific sets of users.)

Now you can execute the script like this:

$ ./

Scala Quicksheet

Description Code
Assign immutable int value val x = 3
Assign mutable int value var x = 3
Assign immutable value with explicit type val x: Int = 27
Assign lazily evaluated value lazy val y = print("Sleeping in.")
Bind a function to a name val f = (x: Int) => x * x
Bind a function to a name with explicit type val f: Int => Int = (x: Int) => x * x
Define a method def f(x: Int) = x * x
Define a method with explicit typing def f(x: Int): Int = x * x
[Define a class][3] class Hopper(someParam: Int) { ... }
[Define an object][3] object Hopper(someParam: Int) { ... }
[Define a trait][4] trait Grace { ... }
[Get first element of sequence][5] Seq(1,2,3).head
[If switch][6] val result = if(x > 0) "Positive!"
[Get all elements of sequence except first][5] Seq(1,2,3).tail
[Loop through a list][7] for { x <- Seq(1,2,3) } print(x)
[Nested Looping][8] for {
  x <- Seq(1,2,3)
  y <- Seq(4,5,6)
} print(x + “:” + y)
[For each list element execute function][9] List(1,2,3).foreach { println }
Print to standard out print("Ada Lovelace")
[Sort a list alphanumerically][10] List('b','c','a').sorted

1:, Val, and Def 2: val [3]: & Companion Objects [4]: [5]: [6]: If Expressions [7]: [8]: For Loop [9]: [10]: A List

Using the Scala REPL

When you execute scala in a terminal without additional parameters it opens up a REPL (Read-Eval-Print Loop) interpreter:

nford:~ $ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
Type in expressions for evaluation. Or try :help.


The REPL allows you to execute Scala in a worksheet fashion: the execution context is preserved and you can manually try out commands without having to build a whole program. For instance, by typing val poem = "As halcyons we shall be" would look like this:

scala> val poem = "As halcyons we shall be"
poem: String = As halcyons we shall be

Now we can print our val:

scala> print(poem)
As halcyons we shall be

Note that val is immutable and cannot be overwritten:

scala> poem = "Brooding on the open sea"
<console>:12: error: reassignment to val
       poem = "Brooding on the open sea"

But in the REPL you can redefine a val (which would cause an error in a normal Scala program, if it was done in the same scope):

scala> val poem = "Brooding on the open sea"
poem: String = Brooding on the open sea

For the remainder of your REPL session this newly defined variable will shadow the previously defined variable. REPLs are useful for quickly seeing how objects or other code works. All of Scala’s features are available: you can define functions, classes, methods, etc.