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
Copy file name to clipboardExpand all lines: _overviews/scala3-book/first-look-at-types.md
+68-12
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,13 @@ next-page: control-structures
8
8
---
9
9
10
10
11
-
12
11
## All values have a type
13
12
14
13
In Scala, all values have a type, including numerical values and functions.
15
14
The diagram below illustrates a subset of the type hierarchy.
16
15
17
16
<ahref="{{ site.baseurl }}/resources/images/scala3-book/hierarchy.svg"><imgstyle="width:100%"src="{{ site.baseurl }}/resources/images/scala3-book/hierarchy.svg"alt="Scala 3 Type Hierarchy"></a>
18
17
19
-
20
18
## Scala type hierarchy
21
19
22
20
`Any` is the supertype of all types, also called the **top type**.
@@ -42,12 +40,18 @@ If Scala is used in the context of a Java runtime environment, `AnyRef` correspo
42
40
In statement-based languages, `void` is used for methods that don’t return anything.
43
41
If you write methods in Scala that have no return value, such as the following method, `Unit` is used for the same purpose:
44
42
43
+
{% tabs unit %}
44
+
{% tab 'Scala 2 and 3' for=unit %}
45
45
```scala
46
46
defprintIt(a: Any):Unit= println(a)
47
47
```
48
+
{% endtab %}
49
+
{% endtabs %}
48
50
49
51
Here’s an example that demonstrates that strings, integers, characters, boolean values, and functions are all instances of `Any` and can be treated just like every other object:
50
52
53
+
{% tabs any %}
54
+
{% tab 'Scala 2 and 3' for=any %}
51
55
```scala
52
56
vallist:List[Any] =List(
53
57
"a string",
@@ -59,6 +63,8 @@ val list: List[Any] = List(
59
63
60
64
list.foreach(element => println(element))
61
65
```
66
+
{% endtab %}
67
+
{% endtabs %}
62
68
63
69
The code defines a value `list` of type `List[Any]`.
64
70
The list is initialized with elements of various types, but each is an instance of `scala.Any`, so we can add them to the list.
@@ -78,6 +84,8 @@ true
78
84
As shown above, Scala’s numeric types extend `AnyVal`, and they’re all full-blown objects.
79
85
These examples show how to declare variables of these numeric types:
80
86
87
+
{% tabs anyval %}
88
+
{% tab 'Scala 2 and 3' for=anyval %}
81
89
```scala
82
90
valb:Byte=1
83
91
vali:Int=1
@@ -86,31 +94,45 @@ val s: Short = 1
86
94
vald:Double=2.0
87
95
valf:Float=3.0
88
96
```
97
+
{% endtab %}
98
+
{% endtabs %}
89
99
90
100
In the first four examples, if you don’t explicitly specify a type, the number `1` will default to an `Int`, so if you want one of the other data types---`Byte`, `Long`, or `Short`---you need to explicitly declare those types, as shown.
91
101
Numbers with a decimal (like 2.0) will default to a `Double`, so if you want a `Float` you need to declare a `Float`, as shown in the last example.
92
102
93
103
Because `Int` and `Double` are the default numeric types, you typically create them without explicitly declaring the data type:
94
104
105
+
{% tabs anynum %}
106
+
{% tab 'Scala 2 and 3' for=anynum %}
95
107
```scala
96
108
vali=123// defaults to Int
97
109
valx=1.0// defaults to Double
98
110
```
111
+
{% endtab %}
112
+
{% endtabs %}
99
113
100
114
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:
101
115
116
+
{% tabs type-post %}
117
+
{% tab 'Scala 2 and 3' for=type-post %}
102
118
```scala
103
119
valx=1_000L// val x: Long = 1000
104
120
valy=2.2D// val y: Double = 2.2
105
121
valz=3.3F// val z: Float = 3.3
106
122
```
123
+
{% endtab %}
124
+
{% endtabs %}
107
125
108
126
Scala also has `String` and `Char` types, which you can generally declare with the implicit form:
109
127
128
+
{% tabs type-string %}
129
+
{% tab 'Scala 2 and 3' for=type-string %}
110
130
```scala
111
131
vals="Bill"
112
132
valc='a'
113
133
```
134
+
{% endtab %}
135
+
{% endtabs %}
114
136
115
137
As shown, enclose strings in double-quotes---or triple-quotes for multiline strings---and enclose a character in single-quotes.
116
138
@@ -128,28 +150,32 @@ Those data types and their ranges are:
128
150
| Char | 16-bit unsigned Unicode character (0 to 2^16-1, inclusive)<br/>0 to 65,535 |
129
151
| String | a sequence of `Char`|
130
152
131
-
132
-
133
153
## `BigInt` and `BigDecimal`
134
154
135
155
When you need really large numbers, use the `BigInt` and `BigDecimal` types:
136
156
157
+
{% tabs type-bigint %}
158
+
{% tab 'Scala 2 and 3' for=type-bigint %}
137
159
```scala
138
160
vala=BigInt(1_234_567_890_987_654_321L)
139
161
valb=BigDecimal(123_456.789)
140
162
```
163
+
{% endtab %}
164
+
{% endtabs %}
141
165
142
166
Where `Double` and `Float` are approximate decimal numbers, `BigDecimal` is used for precise arithmetic, such as when working with currency.
143
167
144
168
A great thing about `BigInt` and `BigDecimal` is that they support all the operators you’re used to using with numeric types:
0 commit comments