Hello World in Scala

Our first program will print the classic “hello world” message. Here’s the full source code.

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("hello world")
  }
}

To run the program, save the code in a file named HelloWorld.scala and use scala to execute it.

$ scala HelloWorld.scala
hello world

Sometimes we’ll want to build our programs into binary files. In Scala, this usually means compiling it into a .class or a JAR (Java ARchive) file.

First, compile the code using scalac:

$ scalac HelloWorld.scala
$ ls
HelloWorld$.class HelloWorld.class HelloWorld.scala

We can then execute the compiled code using the scala command:

$ scala HelloWorld
hello world

Now that we can run and build basic Scala programs, let’s learn more about the language.