Scala Program to Print Hello World

Posted in

Sangeeta Gulia
Last updated on January 12, 2023

    The most basic and the first program we all implement while learning any programming language is “Hello World”. This means printing simply “Hello World” on the screen.

    In this guide, we are going to write our first program in Scala.

    Scala Program To Print 'Hello World'

    In many programming languages, the main method is the entry point of the program. The same is true for the Scala programming language.

    However, Scala also provides you with a higher level of abstraction on the main method in which the object can extend the “App” trait, which will define the entry point of the application. We are going to see examples using both approaches.

    1. Hello World Program Using the Main Method

    objectHelloWorld {
    
    // Main defines the entry point of any program
    defmain(args: Array[String]): Unit = {
    // println can be used to print anything on the standard output i.e., console
    println("Hello World")
    }
    
    }

    Output

    HelloWorld

    In the above example, we created a class with the name HelloWorld.scala. Inside this class, we created an object named HelloWorld. To mark the entry point in this object, we need to define the main method.

    The following is the syntax of the main method:

    defmain(args: Array[String]): Unit

    In the above syntax,

    • def is a keyword used to define methods.
    • Main is the standard method name that denotes the entry point of any program.

    As per the above syntax, the main method takes an argument of type Array of String in the variable named args. If the main program requires any arguments, we need to supply them while running the program, as shown in the following syntax:

    scala program_name argument1 argument2

    In the above example, we are not using arguments. So, we will understand the details around it in some other examples.

    Now coming back to the syntax of the main method, we have “:Unit” at last. This defines the return type of the main method. Unit is similar to void in Java, which means this method does not return anything.

    Next, we used the “println” method that accepts a String-type parameter. This method prints the defined string on the standard output console resulting in the output “Hello World”

    2. Hello World Program Using App Trait

    The App trait can be used to quickly turn objects into executable programs. In this approach, no explicit main method is needed. Instead, the whole class body becomes the “main method”. The args variable returns the current command line arguments as an array if there are any which are supplied to the program.

    objectHelloWorldextendsApp {
    /* Due to the presence of App trait ,no explicit main method is needed. 
    Instead, the whole class body becomes the "main method". 
    */
    
    // println can be used to print anything on the standard output i.e console
    println("Hello World")
    
    }
    

    In the above example, you can also notice how you can write single-line and multi-line comments.

    • For single-line comments, use ‘//’.
    • For multi-line comments, use ‘/* */’.

    Output

    HelloWorld

    How to Run a Scala Program?

    There are many ways you can run a Scala program. I have mentioned a few of them below:

    1. Using Online Compilers

    Online compilers help you run Scala programs without the need to install and configure software programs. To mention a few, you can start with the below options:

    2. Using IDE

    You can use the IntelliJ Idea IDE tool to run Scala programs. To get started, you need to follow these steps:

    • Install IntelliJ IDEA on your system.
    • Install the Scala plugin in IntelliJ IDEA. You can find the option to install under File -> Settings ->Plugins as per the below screenshot.

    IntelliJ IDEA Plugins

    • Write your program and run using the run option(green arrow) available in IDE as below:

    Run Scala Program in IDE

    3. Using Command Line

    To run a Scala program using the command line, you need to follow a two-step process:

    • Compile a scala program. To do so, you can use the below command:
    scalac ObjectName.scala
    • Run the program using the following command.
    scala ObjectName <space separated command_line_arguments(if any)>

    Leave a Comment on this Post

    0 Comments