title | type | description | languages | num | previous-page | next-page | ||
---|---|---|---|---|---|---|---|---|
Hello, World! | section | This section demonstrates a Scala 3 'Hello, World!' example. |
| 5 | taste-intro | taste-repl |
Hint: in the following examples try picking your preferred Scala version.
A Scala “Hello, World!” example goes as follows. First, put this code in a file named hello.scala:
{% tabs hello-world-demo class=tabs-scala-version %}
{% tab 'Scala 2' for=hello-world-demo %}
objecthello { defmain(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).
{% endtab %}
{% tab 'Scala 3' for=hello-world-demo %}
@main defhello() = 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.
{% endtab %}
{% endtabs %}
Next, compile and run the code with scala
:
$ scala run hello.scala
The command should produce an output similar to:
Compiling project (Scala {{site.scala-3-version}}, JVM (20)) Compiled project (Scala {{site.scala-3-version}}, 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][scala_tools] chapter. The Scala CLI documentation can be found here.
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:
{% tabs import-readline %} {% tab 'Scala 2 and 3' for=import-readline %}
importscala.io.StdIn.readLine
{% endtab %} {% endtabs %}
To demonstrate how this works, let’s create a little example. Put this source code in a file named helloInteractive.scala:
{% tabs hello-world-interactive class=tabs-scala-version %}
{% tab 'Scala 2' for=hello-world-interactive %}
importscala.io.StdIn.readLineobjecthelloInteractive { defmain(args: Array[String]) = { println("Please enter your name:") valname= readLine() println("Hello, "+ name +"!") } }
{% endtab %}
{% tab 'Scala 3' for=hello-world-interactive %}
importscala.io.StdIn.readLine@main defhelloInteractive() = println("Please enter your name:") valname= readLine() println("Hello, "+ name +"!")
{% endtab %}
{% endtabs %}
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 {{site.scala-3-version}}, JVM (20)) Compiled project (Scala {{site.scala-3-version}}, 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 {{site.scala-3-version}}, JVM (20)) Compiled project (Scala {{site.scala-3-version}}, JVM (20)) Please enter your name: Alvin Alexander Hello, Alvin Alexander!
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:
{% tabs import-readline-2 %} {% tab 'Scala 2 and 3' for=import-readline-2 %}
importscala.io.StdIn.readLine
{% endtab %} {% endtabs %}
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).
[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %}