|
| 1 | +--- |
| 2 | +layout: multipage-overview |
| 3 | +title: Пример 'Hello, World!' |
| 4 | +scala3: true |
| 5 | +partof: scala3-book |
| 6 | +overview-name: "Scala 3 — Book" |
| 7 | +type: chapter |
| 8 | +description: В этом примере демонстрируется пример 'Hello, World!' на Scala 3. |
| 9 | +language: ru |
| 10 | +num: 5 |
| 11 | +previous-page: taste-intro |
| 12 | +next-page: |
| 13 | +--- |
| 14 | + |
| 15 | +> **Hint**: in the following examples try picking your preferred Scala version. |
| 16 | +> <noscript><span style="font-weight: bold;">Info</span>: JavaScript is currently disabled, code tabs will still work, but preferences will not be remembered.</noscript> |
| 17 | +
|
| 18 | +## Your First Scala Program |
| 19 | + |
| 20 | + |
| 21 | +A Scala “Hello, World!” example goes as follows. |
| 22 | +First, put this code in a file named _hello.scala_: |
| 23 | + |
| 24 | + |
| 25 | +<!-- Display Hello World for each Scala Version --> |
| 26 | +{% tabs hello-world-demo class=tabs-scala-version %} |
| 27 | + |
| 28 | +{% tab 'Scala 2' for=hello-world-demo %} |
| 29 | +```scala |
| 30 | +object hello { |
| 31 | + def main(args: Array[String]) = { |
| 32 | + println("Hello, World!") |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | +> In this code, we defined a method named `main`, inside a Scala `object` named `hello`. |
| 37 | +> An `object` in Scala is similar to a `class`, but defines a singleton instance that you can pass around. |
| 38 | +> `main` takes an input parameter named `args` that must be typed as `Array[String]`, (ignore `args` for now). |
| 39 | +
|
| 40 | +{% endtab %} |
| 41 | + |
| 42 | +{% tab 'Scala 3' for=hello-world-demo %} |
| 43 | +```scala |
| 44 | +@main def hello() = println("Hello, World!") |
| 45 | +``` |
| 46 | +> In this code, `hello` is a method. |
| 47 | +> It’s defined with `def`, and declared to be a “main” method with the `@main` annotation. |
| 48 | +> It prints the `"Hello, World!"` string to standard output (STDOUT) using the `println` method. |
| 49 | +
|
| 50 | +{% endtab %} |
| 51 | + |
| 52 | +{% endtabs %} |
| 53 | +<!-- End tabs --> |
| 54 | + |
| 55 | +Next, compile the code with `scalac`: |
| 56 | + |
| 57 | +```bash |
| 58 | +$ scalac hello.scala |
| 59 | +``` |
| 60 | + |
| 61 | +If you’re coming to Scala from Java, `scalac` is just like `javac`, so that command creates several files: |
| 62 | + |
| 63 | +<!-- Display Hello World compiled outputs for each Scala Version --> |
| 64 | +{% tabs hello-world-outputs class=tabs-scala-version %} |
| 65 | + |
| 66 | +{% tab 'Scala 2' for=hello-world-outputs %} |
| 67 | +```bash |
| 68 | +$ ls -1 |
| 69 | +hello$.class |
| 70 | +hello.class |
| 71 | +hello.scala |
| 72 | +``` |
| 73 | +{% endtab %} |
| 74 | + |
| 75 | +{% tab 'Scala 3' for=hello-world-outputs %} |
| 76 | +```bash |
| 77 | +$ ls -1 |
| 78 | +hello$package$.class |
| 79 | +hello$package.class |
| 80 | +hello$package.tasty |
| 81 | +hello.scala |
| 82 | +hello.class |
| 83 | +hello.tasty |
| 84 | +``` |
| 85 | +{% endtab %} |
| 86 | + |
| 87 | +{% endtabs %} |
| 88 | +<!-- End tabs --> |
| 89 | + |
| 90 | +Like Java, the _.class_ files are bytecode files, and they’re ready to run in the JVM. |
| 91 | + |
| 92 | +Now you can run the `hello` method with the `scala` command: |
| 93 | + |
| 94 | +```bash |
| 95 | +$ scala hello |
| 96 | +Hello, World! |
| 97 | +``` |
| 98 | + |
| 99 | +Assuming that worked, congratulations, you just compiled and ran your first Scala application. |
| 100 | + |
| 101 | +> More information about sbt and other tools that make Scala development easier can be found in the [Scala Tools][scala_tools] chapter. |
| 102 | +
|
| 103 | +## Ask For User Input |
| 104 | + |
| 105 | +In our next example let's ask for the user's name before we greet them! |
| 106 | + |
| 107 | +There are several ways to read input from a command-line, but a simple way is to use the |
| 108 | +`readLine` method in the _scala.io.StdIn_ object. To use it, you need to first import it, like this: |
| 109 | + |
| 110 | +{% tabs import-readline %} |
| 111 | +{% tab 'Scala 2 and 3' for=import-readline %} |
| 112 | +```scala |
| 113 | +import scala.io.StdIn.readLine |
| 114 | +``` |
| 115 | +{% endtab %} |
| 116 | +{% endtabs %} |
| 117 | + |
| 118 | +To demonstrate how this works, let’s create a little example. Put this source code in a file named _helloInteractive.scala_: |
| 119 | + |
| 120 | +<!-- Display interactive Hello World application for each Scala Version --> |
| 121 | +{% tabs hello-world-interactive class=tabs-scala-version %} |
| 122 | + |
| 123 | +{% tab 'Scala 2' for=hello-world-interactive %} |
| 124 | +```scala |
| 125 | +import scala.io.StdIn.readLine |
| 126 | + |
| 127 | +object helloInteractive { |
| 128 | + |
| 129 | + def main(args: Array[String]) = { |
| 130 | + println("Please enter your name:") |
| 131 | + val name = readLine() |
| 132 | + |
| 133 | + println("Hello, " + name + "!") |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | +``` |
| 138 | +{% endtab %} |
| 139 | + |
| 140 | +{% tab 'Scala 3' for=hello-world-interactive %} |
| 141 | +```scala |
| 142 | +import scala.io.StdIn.readLine |
| 143 | + |
| 144 | +@main def helloInteractive() = |
| 145 | + println("Please enter your name:") |
| 146 | + val name = readLine() |
| 147 | + |
| 148 | + println("Hello, " + name + "!") |
| 149 | +``` |
| 150 | +{% endtab %} |
| 151 | + |
| 152 | +{% endtabs %} |
| 153 | +<!-- End tabs --> |
| 154 | + |
| 155 | +In this code we save the result of `readLine` to a variable called `name`, we then |
| 156 | +use the `+` operator on strings to join `"Hello, "` with `name` and `"!"`, making one single string value. |
| 157 | + |
| 158 | +> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html). |
| 159 | +
|
| 160 | +Then compile the code with `scalac`: |
| 161 | + |
| 162 | +```bash |
| 163 | +$ scalac helloInteractive.scala |
| 164 | +``` |
| 165 | +Then run it with `scala helloInteractive`, this time the program will pause after asking for your name, |
| 166 | +and wait until you type a name and press return on the keyboard, looking like this: |
| 167 | + |
| 168 | +```bash |
| 169 | +$ scala helloInteractive |
| 170 | +Please enter your name: |
| 171 | +▌ |
| 172 | +``` |
| 173 | + |
| 174 | +When you enter your name at the prompt, the final interaction should look like this: |
| 175 | + |
| 176 | +```bash |
| 177 | +$ scala helloInteractive |
| 178 | +Please enter your name: |
| 179 | +Alvin Alexander |
| 180 | +Hello, Alvin Alexander! |
| 181 | +``` |
| 182 | + |
| 183 | +### A Note about Imports |
| 184 | + |
| 185 | +As you saw in this application, sometimes certain methods, or other kinds of definitions that we'll see later, |
| 186 | +are not available unless you use an `import` clause like so: |
| 187 | + |
| 188 | +{% tabs import-readline-2 %} |
| 189 | +{% tab 'Scala 2 and 3' for=import-readline-2 %} |
| 190 | +```scala |
| 191 | +import scala.io.StdIn.readLine |
| 192 | +``` |
| 193 | +{% endtab %} |
| 194 | +{% endtabs %} |
| 195 | + |
| 196 | +Imports help you write code in a few ways: |
| 197 | + - you can put code in multiple files, to help avoid clutter, and to help navigate large projects. |
| 198 | + - you can use a code library, perhaps written by someone else, that has useful functionality |
| 199 | + - you can know where a certain definition comes from (especially if it was not written in the current file). |
| 200 | + |
| 201 | +[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %} |
0 commit comments