Scala (programming Language) - Examples - "Hello World" Example

"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:

    The world is nothing, the man is all; in yourself is the law of all nature, and you know not yet how a globule of sap ascends; in yourself slumbers the whole of Reason; it is for you to know all, it is for you to dare all.
    Ralph Waldo Emerson (1803–1882)