"Hello World" Example
Here is the classic Hello World program written in Scala:
object HelloWorld extends App { println("Hello, World!") }Unlike the stand-alone Hello World application for Java, there is no class declaration and nothing is declared to be static; a singleton object created with the object keyword is used instead.
With the program saved in a file named HelloWorld.scala
, it can be compiled from the command line:
$ scalac HelloWorld.scala
To run it:
$ scala HelloWorld
This is analogous to the process for compiling and running Java code. Indeed, Scala's compilation and execution model is identical to that of Java, making it compatible with Java build tools such as Ant.
A shorter version of the "Hello World" Scala program is:
println("Hello, World!")Saved in a file named HelloWorld2.scala
, this can be run as a script without prior compilation using:
$ scala HelloWorld2.scala
Commands can also be fed directly into the Scala interpreter, using the option -e:
$ scala -e 'println("Hello, World!")'
Read more about this topic: Scala (programming Language), Examples
Famous quotes containing the word world:
“Even a minor event in the life of a child is an event of that childs world and thus a world event.”
—Gaston Bachelard (18841962)