|
2 | 2 | title: Your First Lines of Scala
|
3 | 3 | layout: inner-page-no-masthead
|
4 | 4 | includeTOC: true
|
| 5 | +redirect_to: |
| 6 | + - https://docs.scala-lang.org/overviews/scala-book/hello-world-1.html |
5 | 7 | ---
|
6 |
| - |
7 |
| -## Your first lines of code |
8 |
| - |
9 |
| -### The "Hello, world!" Program |
10 |
| - |
11 |
| -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. |
12 |
| - |
13 |
| - object HelloWorld { |
14 |
| - def main(args: Array[String]): Unit = { |
15 |
| - println("Hello, world!") |
16 |
| - } |
17 |
| - } |
18 |
| - |
19 |
| -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. |
20 |
| - |
21 |
| -We assume that both the [Scala software]({{ site.baseurl }}/download) and the user environment are set up correctly. For example: |
22 |
| - |
23 |
| -| Environment | Variable | Value (example) |
24 |
| -|:------------|:-----------------|:--------------- |
25 |
| -| Unix | `$SCALA_HOME` | `/usr/local/share/scala` |
26 |
| -| | `$PATH` | `$PATH:$SCALA_HOME/bin` |
27 |
| -| Windows | `%SCALA_HOME%` | `c:\Progra~1\Scala` |
28 |
| -| | `%PATH%` | `%PATH%;%SCALA_HOME%\bin` |
29 |
| - |
30 |
| - |
31 |
| -### Run it interactively! |
32 |
| - |
33 |
| -The `scala` command starts an interactive shell where Scala expressions are interpreted interactively. |
34 |
| - |
35 |
| - > scala |
36 |
| - This is a Scala shell. |
37 |
| - Type in expressions to have them evaluated. |
38 |
| - Type :help for more information. |
39 |
| - |
40 |
| - scala> object HelloWorld { |
41 |
| - | def main(args: Array[String]): Unit = { |
42 |
| - | println("Hello, world!") |
43 |
| - | } |
44 |
| - | } |
45 |
| - defined module HelloWorld |
46 |
| - |
47 |
| - scala> HelloWorld.main(Array()) |
48 |
| - Hello, world! |
49 |
| - |
50 |
| - scala>:q |
51 |
| - > |
52 |
| - |
53 |
| -The shortcut `:q` stands for the internal shell command `:quit` used to exit the interpreter. |
54 |
| - |
55 |
| -### Compile it! |
56 |
| - |
57 |
| -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). |
58 |
| - |
59 |
| - > scalac HelloWorld.scala |
60 |
| - |
61 |
| -By default `scalac` generates the class files into the current working directory. You may specify a different output directory using the `-d` option. |
62 |
| - |
63 |
| - > scalac -d classes HelloWorld.scala |
64 |
| - |
65 |
| - |
66 |
| -### Execute it! |
67 |
| - |
68 |
| -The `scala` command executes the generated bytecode with the appropriate options: |
69 |
| - |
70 |
| - > scala HelloWorld |
71 |
| - |
72 |
| -`scala` allows us to specify command options, such as the `-classpath` (alias `-cp`) option: |
73 |
| - |
74 |
| - > scala -cp classes HelloWorld |
75 |
| - |
76 |
| -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. |
77 |
| - |
78 |
| -Here is how the "Hello, world!" example looks like using the `App` trait: |
79 |
| - |
80 |
| - object HelloWorld extends App { |
81 |
| - println("Hello, world!") |
82 |
| - } |
83 |
| - |
84 |
| -### Script it! |
85 |
| - |
86 |
| -We may also run our example as a shell script or batch command (see the examples in the man pages of the `scala` command). |
87 |
| - |
88 |
| -The [bash](https://www.gnu.org/software/bash/) shell script `script.sh` containing the following Scala code (and shell preamble): |
89 |
| - |
90 |
| - #!/bin/sh |
91 |
| - exec scala "$0" "$@" |
92 |
| - !# |
93 |
| - |
94 |
| - object HelloWorld extends App { |
95 |
| - println("Hello, world!") |
96 |
| - } |
97 |
| - |
98 |
| -can be run directly from the command shell: |
99 |
| - |
100 |
| - > ./script.sh |
101 |
| - |
102 |
| -**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. |
0 commit comments