Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Hello World in Scala

In Scala, writing a simple "Hello, World!" program is straightforward. Below are the steps to create and run such a program.

Using an IDE or Text Editor

  1. Open your favorite IDE or text editor and create a new Scala file, usually with a .scala extension.
  2. Write the following code in that file:
object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, World!")
  }
}
  1. Save the file as HelloWorld.scala.

  2. Open a terminal and navigate to the directory where HelloWorld.scala is saved.

  3. Compile the Scala file using scalac, the Scala compiler:

    scalac HelloWorld.scala
    
  4. This will generate a HelloWorld.class file. You can run it using the scala command:

    scala HelloWorld
    

You should see "Hello, World!" printed to the terminal.

Using a Build Tool like sbt

  1. Create a new sbt project directory and navigate to it.

  2. Place the HelloWorld.scala file inside the src/main/scala/ directory of the sbt project.

  3. Open a terminal and navigate to the sbt project directory.

  4. Run the following command to execute your program:

    sbt run
    

sbt will compile and run your Scala program, and you should see "Hello, World!" printed to the terminal.

Using Scala REPL

  1. Open Scala REPL (Read-Eval-Print Loop) by typing scala into the terminal.

  2. You can directly print "Hello, World!" to the console as follows:

    println("Hello, World!")
    

These are some of the various ways you can write and run a "Hello, World!" program in Scala.