|
| 1 | +--- |
| 2 | +layout: tutorial |
| 3 | +title: Basics |
| 4 | + |
| 5 | +disqus: true |
| 6 | + |
| 7 | +tutorial: scala-tour |
| 8 | +num: 2 |
| 9 | +next-page: unified-types |
| 10 | +previous-page: tour-of-scala |
| 11 | +--- |
| 12 | + |
| 13 | +In this page, we will cover basics of Scala. |
| 14 | + |
| 15 | +## Trying Scala in the Browser |
| 16 | + |
| 17 | +You can run Scala in your browser by using [ScalaFiddle](https://scalafiddle.io). |
| 18 | + |
| 19 | +1. Go to https://scalafiddle.io. |
| 20 | +2. Copy and paste `println("Hello, world!")` to the left pane. |
| 21 | +3. Hit "Run" button and see it prints "Hello, world!" on the right pane. |
| 22 | + |
| 23 | +It is a perfect way for anybody to experiment with a piece of Scala code anywhere, anytime! |
| 24 | + |
| 25 | +## Expressions |
| 26 | + |
| 27 | +Expressions are computable statements. |
| 28 | + |
| 29 | +You can output results of expressions using the `println`. |
| 30 | + |
| 31 | +``` |
| 32 | +println(1) // 1 |
| 33 | +println(1 + 1) // 2 |
| 34 | +println("Hello!") // Hello! |
| 35 | +println("Hello," + " world!") // Hello, world! |
| 36 | +``` |
| 37 | + |
| 38 | +### Values |
| 39 | + |
| 40 | +You can name results of expressions by using the `val` keyword. |
| 41 | + |
| 42 | +``` |
| 43 | +val x = 1 + 1 |
| 44 | +println(x) // 2 |
| 45 | +``` |
| 46 | + |
| 47 | +Named results are called values. For example, in above case, `x` is called a value. Values only hold results of expressions, so they will not re-compute expressions when referenced. |
| 48 | + |
| 49 | +Values cannot be re-assigned. |
| 50 | + |
| 51 | +``` |
| 52 | +val x = 1 + 1 |
| 53 | +x = 3 // This does not compile. |
| 54 | +``` |
| 55 | + |
| 56 | +Types of values can be inferred, but you can also explicitly state types like below. |
| 57 | + |
| 58 | +``` |
| 59 | +val x: Int = 1 + 1 |
| 60 | +``` |
| 61 | + |
| 62 | +### Variables |
| 63 | + |
| 64 | +Variables are similar to values except you can re-assign results they hold. You can define variables with the `var` keyword. |
| 65 | + |
| 66 | +``` |
| 67 | +var x = 1 + 1 |
| 68 | +x = 3 // This compiles because "x" is declared with the "var" keyword. |
| 69 | +println(x) // 3 |
| 70 | +``` |
| 71 | + |
| 72 | +Just like values, you can also explicitly state types of variables like below. |
| 73 | + |
| 74 | +``` |
| 75 | +var x: Int = 1 + 1 |
| 76 | +``` |
| 77 | + |
| 78 | +### Blocks |
| 79 | + |
| 80 | +You can create a single expression out of multiple statements by wrapping them with `{}`. We call them blocks or block expressions. |
| 81 | + |
| 82 | +The result of the last statement of the block will be the result of the overall block. |
| 83 | + |
| 84 | +``` |
| 85 | +println({ |
| 86 | + val x = 1 + 1 |
| 87 | + x + 1 |
| 88 | +}) // 3 |
| 89 | +``` |
| 90 | + |
| 91 | +## Functions |
| 92 | + |
| 93 | +Functions are expressions with parameters. |
| 94 | + |
| 95 | +You can define a function that returns a given integer + 1 like below. |
| 96 | + |
| 97 | +``` |
| 98 | +(x: Int) => x + 1 |
| 99 | +``` |
| 100 | + |
| 101 | +Left hand side of `=>` is a list of parameters and right hand side of `=>` is an expression taking the parameters. |
| 102 | + |
| 103 | +You can also name functions. |
| 104 | + |
| 105 | +``` |
| 106 | +val addOne = (x: Int) => x + 1 |
| 107 | +println(addOne(1)) // 2 |
| 108 | +``` |
| 109 | + |
| 110 | +Or it can take multiple parameters. |
| 111 | + |
| 112 | +``` |
| 113 | +val add = (x: Int, y: Int) => x + y |
| 114 | +println(add(1, 2)) // 3 |
| 115 | +``` |
| 116 | + |
| 117 | +Or it can take no parameters. |
| 118 | + |
| 119 | +``` |
| 120 | +val getTheAnswer = () => 42 |
| 121 | +println(getTheAnswer()) // 42 |
| 122 | +``` |
| 123 | + |
| 124 | +We will cover functions in depth [later](anonymous-function-syntax.md). |
| 125 | + |
| 126 | +## Methods |
| 127 | + |
| 128 | +Methods look and behave very similar to functions, but there are a few key differences between them. |
| 129 | + |
| 130 | +Methods are defined like below with the `def` keyword followed by its name, a list of parameter groups, return type, and an expression. |
| 131 | + |
| 132 | +``` |
| 133 | +def add(x: Int, y: Int): Int = x + y |
| 134 | +println(add(1, 2)) // 3 |
| 135 | +``` |
| 136 | + |
| 137 | +Methods cannot be named with the `val` or `var` keywords. |
| 138 | + |
| 139 | +``` |
| 140 | +def add(x: Int, y: Int): Int = x + y |
| 141 | +val add2 = add // This does not compile. |
| 142 | +var add3 = add // This does not compile either. |
| 143 | +``` |
| 144 | + |
| 145 | +Methods can take multiple parameter groups. |
| 146 | + |
| 147 | +``` |
| 148 | +def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier |
| 149 | +println(addThenMultiply(1, 2)(3)) // 9 |
| 150 | +``` |
| 151 | + |
| 152 | +Or no parameter groups at all. |
| 153 | + |
| 154 | +``` |
| 155 | +def name: String = System.getProperty("name") |
| 156 | +println("Hello, " + name + "!") |
| 157 | +``` |
| 158 | + |
| 159 | +There are some other differences, but for now, you can think of them as something similar to functions. |
| 160 | + |
| 161 | +## Classes |
| 162 | + |
| 163 | +You can define classes with the `class` keyword followed by its name and constructor parameters. |
| 164 | + |
| 165 | +``` |
| 166 | +class Greeter(prefix: String, postfix: String) { |
| 167 | + def greet(name: String): Unit = println(prefix + name + postfix) |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +You can instantiate classes with the `new` keyword. |
| 172 | + |
| 173 | +``` |
| 174 | +val greeter = new Greeter("Hello, ", "!") |
| 175 | +greeter.greet("Scala developer") // Hello, Scala developer! |
| 176 | +``` |
| 177 | + |
| 178 | +We will cover classes in depth [later](classes.md). |
| 179 | + |
| 180 | +## Case Classes |
| 181 | + |
| 182 | +Scala has a special type of class called case class that's immutable and compared by value by default. You can define case classes with the `case class` keyword. |
| 183 | + |
| 184 | +``` |
| 185 | +case class Point(x: Int, y: Int) |
| 186 | +``` |
| 187 | + |
| 188 | +You can instantiate case classes without `new` keyword. |
| 189 | + |
| 190 | +``` |
| 191 | +val point = Point(1, 2) |
| 192 | +val anotherPoint = Point(1, 2) |
| 193 | +val yetAnotherPoint = Point(2, 2) |
| 194 | +``` |
| 195 | + |
| 196 | +And they are compared by values. |
| 197 | + |
| 198 | +``` |
| 199 | +if (point == anotherPoint) { |
| 200 | + println(point + " and " + anotherPoint + " are the same.") |
| 201 | +} else { |
| 202 | + println(point + " and " + anotherPoint + " are different.") |
| 203 | +} |
| 204 | +// Point(1,2) and Point(1,2) are the same. |
| 205 | +
|
| 206 | +if (point == yetAnotherPoint) { |
| 207 | + println(point + " and " + yetAnotherPoint + " are the same.") |
| 208 | +} else { |
| 209 | + println(point + " and " + yetAnotherPoint + " are different.") |
| 210 | +} |
| 211 | +// Point(1,2) and Point(2,2) are different. |
| 212 | +``` |
| 213 | + |
| 214 | +There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with it! We will cover them in depth [later](case-classes.md). |
| 215 | + |
| 216 | +## Objects |
| 217 | + |
| 218 | +Objects are single instances of their own definitions. You can think of them as singletons of their own classes. |
| 219 | + |
| 220 | +You can define objects with the `object` keyword. |
| 221 | + |
| 222 | +``` |
| 223 | +object IdFactory { |
| 224 | + private var counter = 0 |
| 225 | +
|
| 226 | + def create(): Int = { |
| 227 | + counter += 1 |
| 228 | + counter |
| 229 | + } |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +You can access objects by referring its name. |
| 234 | + |
| 235 | +``` |
| 236 | +val newId: Int = IdFactory.create() |
| 237 | +println(newId) // 1 |
| 238 | +val newerId: Int = IdFactory.create() |
| 239 | +println(newerId) // 2 |
| 240 | +``` |
| 241 | + |
| 242 | +We will cover objects in depth [later](singleton-objects.md). |
| 243 | + |
| 244 | +## Traits |
| 245 | + |
| 246 | +Traits define specification of types as signature of fields and methods. |
| 247 | + |
| 248 | +You can define traits with `trait` keyword. |
| 249 | + |
| 250 | +``` |
| 251 | +trait Greeter { |
| 252 | + def greet(name: String): Unit |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +Traits can also have default implementations. |
| 257 | + |
| 258 | +``` |
| 259 | +trait Greeter { |
| 260 | + def greet(name: String): Unit = println("Hello, " + name + "!") |
| 261 | +} |
| 262 | +``` |
| 263 | + |
| 264 | +You can extend traits with the `extends` keyword and override their implementations with the `override` keyword. |
| 265 | + |
| 266 | +``` |
| 267 | +class DefaultGreeter extends Greeter |
| 268 | +
|
| 269 | +class CustomizableGreeter(prefix: String, postfix: String) extends Greeter { |
| 270 | + override def greet(name: String): Unit = { |
| 271 | + println(prefix + name + postfix) |
| 272 | + } |
| 273 | +} |
| 274 | +
|
| 275 | +val greeter = new DefaultGreeter() |
| 276 | +greeter.greet("Scala developer") // Hello, Scala developer! |
| 277 | +
|
| 278 | +val customGreeter = new CustomizableGreeter("How are you, ", "?") |
| 279 | +customGreeter.greet("Scala developer") // How are you, Scala developer? |
| 280 | +``` |
| 281 | + |
| 282 | +We will cover traits in depth [later](traits.md). |
| 283 | + |
| 284 | +## Main Method |
| 285 | + |
| 286 | +Main method is an entry point of a program. |
| 287 | + |
| 288 | +Using objects, you can define main methods like below. |
| 289 | + |
| 290 | +``` |
| 291 | +object Main { |
| 292 | + def main(args: Array[String]): Unit = println("Hello, Scala developer!") |
| 293 | +} |
| 294 | +``` |
0 commit comments