How to write a hello world program in Scala
Scala, short for Scalable Language, combines features of object-oriented and functional languages to improve application scalability and reliability.
Scala is based on Java. Hence, the syntax is similar. We can consider a Scala program as a collection of objects that call each others’ methods.
Code
We will begin by writing the conventional Hello world program in the code snippet below:
object HelloWorld {def main(args: Array[String]): Unit = {println("Hello world")}}
Explanation
-
We first create an
objectnamedHelloWorld. Anobjectcan hold methods and variables that we can use without first creating an instance of a class. It can also hold static members that are not associated with instances of some other class. -
Next, we define a
methodnamedmain. -
maintakes in a single parameter namedargsof typestring array. -
The
unitkeyword refers to the return type of the method.Unitis used when a method does not return anything. It is synonymous withvoidin Java. -
printlnis used to output elements on the screen. Anything within" "will appear on the screen.
Free Resources