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
This section provides a look at Scala variables and data types.
14
13
15
-
16
14
## Two types of variables
17
15
18
16
When you create a new variable in Scala, you declare whether the variable is immutable or mutable:
@@ -38,43 +36,64 @@ When you create a new variable in Scala, you declare whether the variable is imm
38
36
39
37
These examples show how to create `val` and `var` variables:
40
38
39
+
{% tabs var-express-1 class=tabs-scala-version %}
40
+
{% tab 'Scala 2 and 3' for=var-express-1 %}
41
+
41
42
```scala
42
43
// immutable
43
44
vala=0
44
45
45
46
// mutable
46
47
varb=1
47
48
```
49
+
{% endtab %}
50
+
{% endtabs %}
48
51
49
52
In an application, a `val` can’t be reassigned.
50
53
You’ll cause a compiler error if you try to reassign one:
51
54
55
+
{% tabs var-express-2 class=tabs-scala-version %}
56
+
{% tab 'Scala 2 and 3' for=var-express-2 %}
57
+
52
58
```scala
53
59
valmsg="Hello, world"
54
60
msg ="Aloha"// "reassignment to val" error; this won’t compile
55
61
```
62
+
{% endtab %}
63
+
{% endtabs %}
56
64
57
65
Conversely, a `var` can be reassigned:
58
66
67
+
{% tabs var-express-3 class=tabs-scala-version %}
68
+
{% tab 'Scala 2 and 3' for=var-express-3 %}
69
+
59
70
```scala
60
71
varmsg="Hello, world"
61
72
msg ="Aloha"// this compiles because a var can be reassigned
62
73
```
63
-
64
-
74
+
{% endtab %}
75
+
{% endtabs %}
65
76
66
77
## Declaring variable types
67
78
68
79
When you create a variable you can explicitly declare its type, or let the compiler infer the type:
69
80
81
+
{% tabs var-express-4 class=tabs-scala-version %}
82
+
{% tab 'Scala 2 and 3' for=var-express-4 %}
83
+
70
84
```scala
71
85
valx:Int=1// explicit
72
86
valx=1// implicit; the compiler infers the type
73
87
```
88
+
{% endtab %}
89
+
{% endtabs %}
74
90
75
91
The second form is known as _type inference_, and it’s a great way to help keep this type of code concise.
76
92
The Scala compiler can usually infer the data type for you, as shown in the output of these REPL examples:
77
93
94
+
{% tabs var-express-5 class=tabs-scala-version %}
95
+
{% tab 'Scala 2 and 3' for=var-express-5 %}
96
+
78
97
```scala
79
98
scala>valx=1
80
99
valx:Int=1
@@ -85,19 +104,24 @@ val s: String = a string
85
104
scala>valnums=List(1, 2, 3)
86
105
valnums:List[Int] =List(1, 2, 3)
87
106
```
107
+
{% endtab %}
108
+
{% endtabs %}
88
109
89
110
You can always explicitly declare a variable’s type if you prefer, but in simple assignments like these it isn’t necessary:
90
111
112
+
{% tabs var-express-6 class=tabs-scala-version %}
113
+
{% tab 'Scala 2 and 3' for=var-express-6 %}
114
+
91
115
```scala
92
116
valx:Int=1
93
117
vals:String="a string"
94
118
valp:Person=Person("Richard")
95
119
```
120
+
{% endtab %}
121
+
{% endtabs %}
96
122
97
123
Notice that with this approach, the code feels more verbose than necessary.
98
124
99
-
100
-
101
125
{% comment %}
102
126
TODO: Jonathan had an early comment on the text below: “While it might feel like this, I would be afraid that people automatically assume from this statement that everything is always boxed.” Suggestion on how to change this?
103
127
{% endcomment %}
@@ -109,6 +133,9 @@ In Scala, everything is an object.
109
133
110
134
These examples show how to declare variables of the numeric types:
111
135
136
+
{% tabs var-express-7 class=tabs-scala-version %}
137
+
{% tab 'Scala 2 and 3' for=var-express-7 %}
138
+
112
139
```scala
113
140
valb:Byte=1
114
141
vali:Int=1
@@ -117,38 +144,59 @@ val s: Short = 1
117
144
vald:Double=2.0
118
145
valf:Float=3.0
119
146
```
147
+
{% endtab %}
148
+
{% endtabs %}
120
149
121
150
Because `Int` and `Double` are the default numeric types, you typically create them without explicitly declaring the data type:
122
151
152
+
{% tabs var-express-8 class=tabs-scala-version %}
153
+
{% tab 'Scala 2 and 3' for=var-express-8 %}
154
+
123
155
```scala
124
156
vali=123// defaults to Int
125
157
valj=1.0// defaults to Double
126
158
```
159
+
{% endtab %}
160
+
{% endtabs %}
127
161
128
162
In your code you can also append the characters `L`, `D`, and `F` (and their lowercase equivalents) to numbers to specify that they are `Long`, `Double`, or `Float` values:
129
163
164
+
{% tabs var-express-9 class=tabs-scala-version %}
165
+
{% tab 'Scala 2 and 3' for=var-express-9 %}
166
+
130
167
```scala
131
168
valx=1_000L// val x: Long = 1000
132
169
valy=2.2D// val y: Double = 2.2
133
170
valz=3.3F// val z: Float = 3.3
134
171
```
172
+
{% endtab %}
173
+
{% endtabs %}
135
174
136
175
When you need really large numbers, use the `BigInt` and `BigDecimal` types:
0 commit comments