Hint: in the following examples try picking your preferred Scala version.
Your First Scala Program
A Scala “Hello, World!” example goes as follows. First, put this code in a file named hello.scala:
object hello {
def main(args: Array[String]) = {
println("Hello, World!")
}
}
In this code, we defined a method named
main
, inside a Scalaobject
namedhello
. Anobject
in Scala is similar to aclass
, but defines a singleton instance that you can pass around.main
takes an input parameter namedargs
that must be typed asArray[String]
, (ignoreargs
for now).
@main def hello() = println("Hello, World!")
In this code,
hello
is a method. It’s defined withdef
, and declared to be a “main” method with the@main
annotation. It prints the"Hello, World!"
string to standard output (STDOUT) using theprintln
method.
Next, compile and run the code with scala
:
$ scala run hello.scala
The command should produce an output similar to:
Compiling project (Scala 3.6.2, JVM (20))
Compiled project (Scala 3.6.2, JVM (20))
Hello, World!
Assuming that worked, congratulations, you just compiled and ran your first Scala application.
More information about sbt and other tools that make Scala development easier can be found in the Scala Tools chapter. The Scala CLI documentation can be found here.
Ask For User Input
In our next example let’s ask for the user’s name before we greet them!
There are several ways to read input from a command-line, but a simple way is to use the
readLine
method in the scala.io.StdIn object. To use it, you need to first import it, like this:
import scala.io.StdIn.readLine
To demonstrate how this works, let’s create a little example. Put this source code in a file named helloInteractive.scala:
import scala.io.StdIn.readLine
object helloInteractive {
def main(args: Array[String]) = {
println("Please enter your name:")
val name = readLine()
println("Hello, " + name + "!")
}
}
import scala.io.StdIn.readLine
@main def helloInteractive() =
println("Please enter your name:")
val name = readLine()
println("Hello, " + name + "!")
In this code we save the result of readLine
to a variable called name
, we then
use the +
operator on strings to join "Hello, "
with name
and "!"
, making one single string value.
You can learn more about using
val
by reading Variables and Data Types.
Then run the code with scala
. This time the program will pause after asking for your name,
and wait until you type a name and press return on the keyboard, looking like this:
$ scala run helloInteractive.scala
Compiling project (Scala 3.6.2, JVM (20))
Compiled project (Scala 3.6.2, JVM (20))
Please enter your name:
▌
When you enter your name at the prompt, the final interaction should look like this:
$ scala run helloInteractive.scala
Compiling project (Scala 3.6.2, JVM (20))
Compiled project (Scala 3.6.2, JVM (20))
Please enter your name:
Alvin Alexander
Hello, Alvin Alexander!
A Note about Imports
As you saw in this application, sometimes certain methods, or other kinds of definitions that we’ll see later,
are not available unless you use an import
clause like so:
import scala.io.StdIn.readLine
Imports help you write code in a few ways:
- you can put code in multiple files, to help avoid clutter, and to help navigate large projects.
- you can use a code library, perhaps written by someone else, that has useful functionality
- you can know where a certain definition comes from (especially if it was not written in the current file).