helloWorld.scala

From GM-RKB
Jump to navigation Jump to search

helloWorld.scala is a Hello World Program that is a Scala program.



References

2013

Here is the classic Hello World program written in Scala:

<source lang=Scala>
 object HelloWorld extends App {
   println("Hello, World!")
 }
</source>

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 when 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 (You may need to use the "-cp" key to set the classpath like in Java).

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: <source lang=Scala> println("Hello, World!") </source> 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!")'