Scala

7 / 53

Scala - Assessment - Hello, World! Program

Let's write a "Hello, World!" program to learn how to compile and run a Scala code in the console prompt.

This exercise has been included to get familiar with the basic way of compiling and running the Scala code.

Later on, we will be using Jupyter notebook to do the same which is easier and faster.

INSTRUCTIONS
  • Click on Console tab on the right-hand side to get the operating system prompt.

  • Create a directory 'scala' in your home directory and go inside it. Create a file hello_world.scala using the command "nano hello_world.scala". To achieve these tasks, run below commands on the operating system prompt.

    mkdir -p scala
    cd scala
    nano hello_world.scala
  • You can also use vim editor instead of nano. Type the code given below. Press "control o" to save the file and then press enter. Press "control x" to exit the nano editor.
object HelloWorld {
    def main(args: Array[String]) {
        println("Hello, World!")
    } 
}
  • To compile the code, type "scalac hello_world.scala" and press Enter.
scalac hello_world.scala

You can see two class files HelloWorld$.class, HelloWorld.class. These .class files are generated as bytecode which gets executed by JVM.

  • To run the code, type "scala HelloWorld". You can see that "Hello, World!" is printed on the screen.
scala HelloWorld
  • Click on "Submit Answer"


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...