Scala - Hello World Program



Scala programming language supports JavaScript as a compilation target. Scala programs can run in Nodejs and in the web browser.

First Scala Program - Hello World

You should be comfortable writing a Hello, World program using the programming language that you use. This is the first and basic program in the Scala programming language.

This program can be divided into different parts.

Hello World Program Code

// Scala program to print Hello World! 
object HelloWorld { 

   // Main Method 
   def main(args: Array[String]){ 
   
      // prints Hello World 
      println("Hello World!") 
   } 
} 

Explanation of HelloWorld Program

Note that this program mainly has three important parts, these are explained as follows below.

  • object HelloWorld − This is the keyword which creates the objects. HelloWorld is the name of the object here. Objects are the instance of a class.
  • def main(args: Array[String]) − `def` is a keyword which defines a function in Scala. `main` and `Array[String]` are the name of the method and for command line arguments respectively.
  • println("Hello World!") − This is a method which displays output on the console.

Running a Scala Hello World Program

You need to save this file HelloWorld.class format and save it. You can compile this program using the `scalac` command which will generate a few class files in the current directory. There will be fine named HelloWorld.class

Use the following command to compile and execute the above Scala program −

\> scalac HelloWorld.scala
\> scala HelloWorld

The output will be −

Hello, World!

Ways to Compile and Execute a Scala Hello World Program

We can execute a Scala program in two modes: one is interactive mode and another is script mode.

1. Interactive Mode

Open the command prompt and use the following command to open Scala.

\>scala

If Scala is installed in your system, the following output will be displayed −

Welcome to Scala version 2.9.0.1
Type in expressions to have them evaluated.
Type :help for more information.

Type the following text to the right of the Scala prompt and press the Enter key −

scala> println("Hello, Scala!");

It will produce the following result −

Hello, Scala!

2. Script Mode

Use the following instructions to write a Scala program in script mode. Open notepad and add the following code into it.

Example

object HelloWorld {
   /* This is my first java program.  
   * This will print 'Hello World' as the output
   */
   def main(args: Array[String]) {
      println("Hello, world!") // prints Hello World
   }
}

Save the file as HelloWorld.scala.

Open the command prompt window and go to the directory where the program file is saved. The ‘scalac’ command is used to compile the Scala program and it will generate a few class files in the current directory. One of them will be called HelloWorld.class. This is a bytecode which will run on Java Virtual Machine (JVM) using ‘scala’ command.

Use the following command to compile and execute your Scala program.

\> scalac HelloWorld.scala
\> scala HelloWorld

The output will be −

Hello, World!

Ways to print "Hello, World!" in Scala 2 and Scala 3

Scala 2

1. Using `println` method

println("Hello, World!")

2. Using `console.println` method

Console.println("Hello, World!")

3. Using printf method

printf("Hello, World!\n")

4. Using a simple function

def printHelloWorld(): Unit = {
   println("Hello, World!")
}

printHelloWorld()

Scala 3

There are syntax improvements in Scala 3 but you can still use Scala 2.

1. Using println method (same as Scala 2)

println("Hello, World!")

2. Using a simple function (same as Scala 2)

def printHelloWorld(): Unit = {
   println("Hello, World!")
}

printHelloWorld()

3. Using given and using for custom output

given output: String = "Hello, World!"
println(using output)

Asking for User Input

You can also ask for user Input for which you need to read input. There are various ways to read input from a command-line line. There is a simple way to use it with the help of `readLine` from `scala.io.StdIn`. You need to first import it.

Example program in Scala 2 −

import scala.io.StdIn.readLine

object temperatureConversion {
   def main(args: Array[String]) = {
      println("Enter temperature in Celsius:")
      val celsius = readLine().toDouble
      val fahrenheit = celsius * 9/5 + 32
      println("Temperature in Fahrenheit: " + fahrenheit)
   }
}

Enter Celsius temperature, and it converts it to Fahrenheit.

We can have above program in Scala 3, like this −

import scala.io.StdIn

@main def temperatureConversion: Unit =
   println("Enter temperature in Celsius:")
   val celsius = StdIn.readLine().toDouble
   val fahrenheit = celsius * 9/5 + 32
   println(s"Temperature in Fahrenheit: $fahrenheit")
Advertisements