Hello World by Defining a 'main' Method
Place this code in a file named HelloWorld.scala:
To compile it to bytecode that is executable by the JVM:
To run it:
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:
And then execute it: