Scala Reactive Programming
上QQ阅读APP看书,第一时间看更新

Understanding the Scala Application

Before discussing Scala Functional Programming features, we will discuss the Scala app. As a Java developer, initially everybody starts with some HelloWorld example, using the main() method. We can do the same thing in Scala using its app (or application).

The Scala app is defined in the scala package as scala.App. Let's develop a HelloWorld example, using IntelliJ IDEA:

  1. Create a Scala SBT project:
      Project Name: scala-helloworld-app 
  1. Create the following Scala app:

HelloWorldApp.scala:

      object HelloWorldApp extends App{ 
        println("Hello Scala World!") 
      } 

Here, scala.App is a trait, which is used to develop Scala standalone applications.

  1. Like Java applications with the main() method, we can write the same Scala Application, as shown here:
      object HelloWorldApp_MainMethod_App { 
        def main(args: Array[String]) = { 
          println("Hello Scala World!") 
        } 
      } 

Here, we are using a Java-like main() method.

  1. When we run both programs, we will get the same output as shown here:
      Hello Scala World! 
  1. By observing both programs, we can understand that scala.App provides the following main() for free. We don't need to use it again:
      def main(args: Array[String]) = { 
      }