Skip to content

Commit 736822e

Browse files
committed
make hello world page language agnostic
1 parent 835ca9b commit 736822e

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

_overviews/scala3-book/taste-hello-world.md

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,45 @@ previous-page: taste-intro
77
next-page: taste-repl
88
---
99

10+
> **Hint**: in the following examples try picking your preferred Scala version.
11+
> <noscript><span style="font-weight: bold;">Info</span>: JavaScript is currently disabled, code tabs will still work, but preferences will not be remembered.</noscript>
12+
1013
## Your First Scala Program
1114

12-
A Scala 3 “Hello, world!” example goes as follows.
15+
16+
A Scala “Hello, World!” example goes as follows.
1317
First, put this code in a file named _hello.scala_:
1418

19+
20+
<!-- Display Hello World for each Scala Version -->
21+
{% tabs hello-world-demo class=tabs-scala-version %}
22+
23+
{% tab 'Scala 2' for=hello-world-demo %}
24+
```scala
25+
object hello {
26+
def main(args: Array[String]) = {
27+
println("Hello, World!")
28+
}
29+
}
30+
```
31+
> In this code, we defined a method named `main`, inside a Scala `object` named `hello`.
32+
> An `object` in Scala is similar to a `class`, but defines a singleton instance that you can pass around.
33+
> `main` takes an input parameter named `args` that must be typed as `Array[String]`, (ignore it for now).
34+
35+
{% endtab %}
36+
37+
{% tab 'Scala 3' for=hello-world-demo %}
1538
```scala
16-
@main def hello() = println("Hello, world!")
39+
@main def hello() = println("Hello, World!")
1740
```
41+
> In this code, `hello` is a method.
42+
> It’s defined with `def`, and declared to be a “main” method with the `@main` annotation.
43+
> It prints the `"Hello, World!"` string to standard output (STDOUT) using the `println` method.
44+
45+
{% endtab %}
1846

19-
In this code, `hello` is a method.
20-
It’s defined with `def`, and declared to be a “main” method with the `@main` annotation.
21-
It prints the `"Hello, world!"` string to standard output (STDOUT) using the `println` method.
47+
{% endtabs %}
48+
<!-- End tabs -->
2249

2350
Next, compile the code with `scalac`:
2451

@@ -28,6 +55,19 @@ $ scalac hello.scala
2855

2956
If you’re coming to Scala from Java, `scalac` is just like `javac`, so that command creates several files:
3057

58+
<!-- Display Hello World compiled outputs for each Scala Version -->
59+
{% tabs hello-world-outputs class=tabs-scala-version %}
60+
61+
{% tab 'Scala 2' for=hello-world-outputs %}
62+
```bash
63+
$ ls -1
64+
hello$.class
65+
hello.class
66+
hello.scala
67+
```
68+
{% endtab %}
69+
70+
{% tab 'Scala 3' for=hello-world-outputs %}
3171
```bash
3272
$ ls -1
3373
hello$package$.class
@@ -37,14 +77,18 @@ hello.scala
3777
hello.class
3878
hello.tasty
3979
```
80+
{% endtab %}
81+
82+
{% endtabs %}
83+
<!-- End tabs -->
4084

4185
Like Java, the _.class_ files are bytecode files, and they’re ready to run in the JVM.
4286

4387
Now you can run the `hello` method with the `scala` command:
4488

4589
```bash
4690
$ scala hello
47-
Hello, world!
91+
Hello, World!
4892
```
4993

5094
Assuming that worked, congratulations, you just compiled and ran your first Scala application.
@@ -64,6 +108,27 @@ import scala.io.StdIn.readLine
64108

65109
To demonstrate how this works, let’s create a little example. Put this source code in a file named _helloInteractive.scala_:
66110

111+
<!-- Display interactive Hello World application for each Scala Version -->
112+
{% tabs hello-world-interactive class=tabs-scala-version %}
113+
114+
{% tab 'Scala 2' for=hello-world-interactive %}
115+
```scala
116+
import scala.io.StdIn.readLine
117+
118+
object helloInteractive {
119+
120+
def main(args: Array[String]) = {
121+
println("Please enter your name:")
122+
val name = readLine()
123+
124+
println("Hello, " + name + "!")
125+
}
126+
127+
}
128+
```
129+
{% endtab %}
130+
131+
{% tab 'Scala 3' for=hello-world-interactive %}
67132
```scala
68133
import scala.io.StdIn.readLine
69134

@@ -73,6 +138,10 @@ import scala.io.StdIn.readLine
73138

74139
println("Hello, " + name + "!")
75140
```
141+
{% endtab %}
142+
143+
{% endtabs %}
144+
<!-- End tabs -->
76145

77146
In this code we save the result of `readLine` to a variable called `name`, we then
78147
use the `+` operator on strings to join `"Hello, "` with `name` and `"!"`, making one single string value.

0 commit comments

Comments
 (0)