You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can name results of expressions by using the `val` keyword.
40
+
You can name results of expressions with the `val` keyword.
41
41
42
42
```
43
43
val x = 1 + 1
44
-
println(x) // 2
44
+
println(x * x) // 4
45
45
```
46
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.
47
+
Named results, such as `x` here, are called values. Referencing
48
+
a value does not re-compute it.
48
49
49
50
Values cannot be re-assigned.
50
51
@@ -53,33 +54,33 @@ val x = 1 + 1
53
54
x = 3 // This does not compile.
54
55
```
55
56
56
-
Types of values can be inferred, but you can also explicitly state types like below.
57
+
Types of values can be inferred, but you can also explicitly state the type, like this:
57
58
58
59
```
59
60
val x: Int = 1 + 1
60
61
```
61
62
62
63
### Variables
63
64
64
-
Variables are similar to values except you can re-assign results they hold. You can define variables with the `var` keyword.
65
+
Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword.
65
66
66
67
```
67
68
var x = 1 + 1
68
69
x = 3 // This compiles because "x" is declared with the "var" keyword.
69
-
println(x) // 3
70
+
println(x * x) // 9
70
71
```
71
72
72
-
Just like values, you can also explicitly state types of variables like below.
73
+
As with values, you can explicitly state the type if you want:
73
74
74
75
```
75
76
var x: Int = 1 + 1
76
77
```
77
78
78
79
### Blocks
79
80
80
-
You can create a single expression out of multiple statements by wrapping them with `{}`. We call them blocks or block expressions.
81
+
You can combine expressions by surrounding them with `{}`. We call this a block.
81
82
82
-
The result of the last statement of the block will be the result of the overall block.
83
+
The result of the last expression in the block is the result of the overall block, too.
83
84
84
85
```
85
86
println({
@@ -90,15 +91,15 @@ println({
90
91
91
92
## Functions
92
93
93
-
Functions are expressions with parameters.
94
+
Functions are expressions that take parameters and compute a result.
94
95
95
-
You can define a function that returns a given integer + 1 like below.
96
+
You can define a function that returns a given integer plus one:
96
97
97
98
```
98
99
(x: Int) => x + 1
99
100
```
100
101
101
-
Left hand side of `=>` is a list of parameters and right hand side of `=>`is an expression taking the parameters.
102
+
On the left of `=>` is a list of parameters. On the right is an expression involving the parameters.
102
103
103
104
You can also name functions.
104
105
@@ -107,7 +108,7 @@ val addOne = (x: Int) => x + 1
107
108
println(addOne(1)) // 2
108
109
```
109
110
110
-
Or it can take multiple parameters.
111
+
Functions may take multiple parameters.
111
112
112
113
```
113
114
val add = (x: Int, y: Int) => x + y
@@ -127,7 +128,7 @@ We will cover functions in depth [later](anonymous-function-syntax.md).
127
128
128
129
Methods look and behave very similar to functions, but there are a few key differences between them.
129
130
130
-
Methods are defined like below with the `def` keywordfollowed by its name, a list of parameter groups, return type, and an expression.
131
+
Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body.
131
132
132
133
```
133
134
def add(x: Int, y: Int): Int = x + y
@@ -142,14 +143,14 @@ val add2 = add // This does not compile.
@@ -163,12 +164,13 @@ There are some other differences, but for now, you can think of them as somethin
163
164
You can define classes with the `class` keyword followed by its name and constructor parameters.
164
165
165
166
```
166
-
class Greeter(prefix: String, postfix: String) {
167
-
def greet(name: String): Unit = println(prefix + name + postfix)
167
+
class Greeter(prefix: String, suffix: String) {
168
+
def greet(name: String): Unit =
169
+
println(prefix + name + suffix)
168
170
}
169
171
```
170
172
171
-
You can instantiate classes with the `new` keyword.
173
+
You can make an instance of a class with the `new` keyword.
172
174
173
175
```
174
176
val greeter = new Greeter("Hello, ", "!")
@@ -179,7 +181,7 @@ We will cover classes in depth [later](classes.md).
179
181
180
182
## Case Classes
181
183
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.
184
+
Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the `case class`keywords.
183
185
184
186
```
185
187
case class Point(x: Int, y: Int)
@@ -193,7 +195,7 @@ val anotherPoint = Point(1, 2)
193
195
val yetAnotherPoint = Point(2, 2)
194
196
```
195
197
196
-
And they are compared by values.
198
+
And they are compared by value.
197
199
198
200
```
199
201
if (point == anotherPoint) {
@@ -222,15 +224,14 @@ You can define objects with the `object` keyword.
222
224
```
223
225
object IdFactory {
224
226
private var counter = 0
225
-
226
227
def create(): Int = {
227
228
counter += 1
228
229
counter
229
230
}
230
231
}
231
232
```
232
233
233
-
You can access objects by referring its name.
234
+
You can access an object by referring to its name.
234
235
235
236
```
236
237
val newId: Int = IdFactory.create()
@@ -243,7 +244,7 @@ We will cover objects in depth [later](singleton-objects.md).
243
244
244
245
## Traits
245
246
246
-
Traits define specification of types as signature of fields and methods.
247
+
Traits are types containing certain fields and methods. Multiple traits can be combined.
247
248
248
249
You can define traits with `trait` keyword.
249
250
@@ -257,11 +258,12 @@ Traits can also have default implementations.
257
258
258
259
```
259
260
trait Greeter {
260
-
def greet(name: String): Unit = println("Hello, " + name + "!")
261
+
def greet(name: String): Unit =
262
+
println("Hello, " + name + "!")
261
263
}
262
264
```
263
265
264
-
You can extend traits with the `extends` keyword and override their implementations with the `override` keyword.
266
+
You can extend traits with the `extends` keyword and override an implementation with the `override` keyword.
265
267
266
268
```
267
269
class DefaultGreeter extends Greeter
@@ -279,16 +281,21 @@ val customGreeter = new CustomizableGreeter("How are you, ", "?")
279
281
customGreeter.greet("Scala developer") // How are you, Scala developer?
280
282
```
281
283
284
+
Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits.
285
+
282
286
We will cover traits in depth [later](traits.md).
283
287
284
288
## Main Method
285
289
286
-
Main method is an entry point of a program.
290
+
The main method is an entry point of a program. The Java Virtual
291
+
Machine requires a main method to be named `main` and take one
292
+
argument, an array of strings.
287
293
288
-
Using objects, you can define main methods like below.
294
+
Using an object, you can define a main method as follows:
289
295
290
296
```
291
297
object Main {
292
-
def main(args: Array[String]): Unit = println("Hello, Scala developer!")
0 commit comments