Scala Language


This draft deletes the entire topic.

expand all collapse all

Examples

  • 42
    object HelloWorld extends App {
      println("Hello, world!")
    }
    

    Live demo

    By extending the App trait, you can avoid defining an explicit main method. The entire body of the HelloWorld object is treated as "the main method".

    2.11.0

    Delayed Initialization

    Per the official documentation, App makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.

    2.11.0

    Delayed Initialization

    Per the official documentation, App makes use of a feature called Delayed Initialization. This means that the object fields are initialized after the main method is called.

    DelayedInit is now deprecated for general use, but is still supported for App as a special case. Support will continue until a replacement feature is decided upon and implemented.

    To access command-line arguments when extending App, use this.args:

    object HelloWorld extends App {
      println("Hello World!")
      for {
        arg <- this.args
      } println(s"Arg=$arg")
    }
    

    When using App you need not override the main method: simply use the body of the class as the main method.

  • 26

    Place this code in a file named HelloWorld.scala:

    object Hello {
      def main(args: Array[String]): Unit = {
        println("Hello World!")
      }
    }
    

    Live demo

    To compile it to bytecode that is executable by the JVM:

    $ scalac HelloWorld.scala
    

    To run it:

    $ scala Hello
    

    When the Scala runtime loads the program, it looks for an object named HelloWorld 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:

    object HelloWorld {
      def main(args: Array[String]): Unit = {
        println("Hello World!")
        for {
          arg <- args
        } println(s"Arg=$arg")
      }
    }
    

    And then execute it:

    $ scalac HelloWorld.scala
    $ scala HelloWorld 1 2 3
    Hello World!
    Arg=1
    Arg=2
    Arg=3
    
  • 7

    Scala can be used as a scripting language. To demonstrate, create HelloWorld.scala with the following content:

    println("Hello")
    

    Execute it with the command-line interpreter (the $ is the command line prompt):

    $ scala HelloWorld.scala
    Hello
    

    If you omit .scala (such as if you simply typed scala HelloWorld) the runner will look for a compiled .class file with bytecode instead of compiling and then executing the script.

    Note: If scala is used as a scripting language no package can be defined.

    In operating systems utilizing bash or similar shell terminals, Scala scripts can be executed using a 'shell preamble'. Create a file named HelloWorld.sh and place the following as it's contents:

    #!/bin/sh
    exec scala "$0" "$@"
    !#
    println("Hello")
    

    The parts between #! and !# is the 'shell preamble', and is interpreted as a bash script. The rest is Scala.

    Once you have saved the above file, you must grant it 'executable' permissions. In the shell you can do this:

    $ chmod a+x HelloWorld.sh
    

    (Note that this gives permission to everyone: read about chmod to learn how to set it for more specific sets of users.)

    Now you can execute the script like this:

    $ ./HelloWorld.sh
    
Please consider making a request to improve this example.

Remarks

Most given examples require a working Scala installation. This is the Scala installation page, and this is the 'How to setup Scala' example. scalafiddle.net is a good resource for executing small code examples over the web.

Versions

VersionRelease Date
2.10.12013-03-13
2.10.22013-06-06
2.10.32013-10-01
2.10.42014-03-24
2.10.52015-03-05
2.10.62015-09-18
2.11.02014-04-21
2.11.12014-05-21
2.11.22014-07-24
2.11.42014-10-30
2.11.52014-01-14
2.11.62015-03-05
2.11.72015-06-23
2.11.82016-03-08
2.12.02016-10-28
Still have a question about Getting started with Scala Language? Ask Question

Topic Outline