Skip to content

Remove page “Your First Lines of Scala” #1323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 2 additions & 97 deletions documentation/your-first-lines-of-scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,6 @@
title: Your First Lines of Scala
layout: inner-page-no-masthead
includeTOC: true
redirect_to:
- https://docs.scala-lang.org/overviews/scala-book/hello-world-1.html
---

## Your first lines of code

### The "Hello, world!" Program

As a first example, we use the standard "Hello, world!" program to demonstrate the use of the Scala tools without knowing too much about the language.

object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}

The structure of this program should be familiar to Java programmers: it consists of the method `main` which prints out a friendly greeting to the standard output.

We assume that both the [Scala software]({{ site.baseurl }}/download) and the user environment are set up correctly. For example:

| Environment | Variable | Value (example)
|:------------|:-----------------|:---------------
| Unix | `$SCALA_HOME` | `/usr/local/share/scala`
| | `$PATH` | `$PATH:$SCALA_HOME/bin`
| Windows | `%SCALA_HOME%` | `c:\Progra~1\Scala`
| | `%PATH%` | `%PATH%;%SCALA_HOME%\bin`


### Run it interactively!

The `scala` command starts an interactive shell where Scala expressions are interpreted interactively.

> scala
This is a Scala shell.
Type in expressions to have them evaluated.
Type :help for more information.

scala> object HelloWorld {
| def main(args: Array[String]): Unit = {
| println("Hello, world!")
| }
| }
defined module HelloWorld

scala> HelloWorld.main(Array())
Hello, world!

scala>:q
>

The shortcut `:q` stands for the internal shell command `:quit` used to exit the interpreter.

### Compile it!

The `scalac` command compiles one (or more) Scala source file(s) and generates Java bytecode which can be executed on any [standard JVM](https://java.sun.com/docs/books/jvms/). The Scala compiler works similarly to `javac`, the Java compiler of the [Java SDK](https://www.oracle.com/technetwork/java/index.html).

> scalac HelloWorld.scala

By default `scalac` generates the class files into the current working directory. You may specify a different output directory using the `-d` option.

> scalac -d classes HelloWorld.scala


### Execute it!

The `scala` command executes the generated bytecode with the appropriate options:

> scala HelloWorld

`scala` allows us to specify command options, such as the `-classpath` (alias `-cp`) option:

> scala -cp classes HelloWorld

The argument of the `scala` command has to be a top-level object. If that object extends trait scala.App, then all statements contained in that object will be executed; otherwise you have to add a method `main` which will act as the entry point of your program.

Here is how the "Hello, world!" example looks like using the `App` trait:

object HelloWorld extends App {
println("Hello, world!")
}

### Script it!

We may also run our example as a shell script or batch command (see the examples in the man pages of the `scala` command).

The [bash](https://www.gnu.org/software/bash/) shell script `script.sh` containing the following Scala code (and shell preamble):

#!/bin/sh
exec scala "$0" "$@"
!#

object HelloWorld extends App {
println("Hello, world!")
}

can be run directly from the command shell:

> ./script.sh

**Note**: We assume here that the file `script.sh` has execute permission and the search path for the `scala` command is specified in the `PATH` environment variable.
3 changes: 1 addition & 2 deletions download/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ For quick access, add `scala` and `scalac` to your path. For example:

## Get Started with Scala

Check out
[our guide to taking your first steps in Scala]({{ site.baseurl }}/documentation/your-first-lines-of-scala.html).
Check out [our guide to writing a "Hello, world" example](https://docs.scala-lang.org/overviews/scala-book/hello-world-1.html).