Scala Data Types
Every value in Scala has a type, and every type is a class — even numbers and booleans have methods. There are no "primitive" types at the language level; the compiler still turns Int, Double and friends into fast JVM primitives behind the scenes.
The basic value types
| Type | Size | Range / values | Literal example |
|---|---|---|---|
Byte |
8-bit | −128 to 127 | val b: Byte = 10 |
Short |
16-bit | −32,768 to 32,767 | val s: Short = 300 |
Int |
32-bit | about ±2.1 billion | 42 |
Long |
64-bit | about ±9.2 × 10¹⁸ | 42L |
Float |
32-bit | ~7 decimal digits | 3.14f |
Double |
64-bit | ~15 decimal digits | 3.14 |
Char |
16-bit | one UTF-16 character | 'A' |
Boolean |
— | true or false |
true |
Unit |
— | only the value () |
() |
String |
— | text (a java.lang.String) |
"hi" |
@main def run(): Unit =
val small: Byte = 120
val count = 2_000_000_000
val distance = 9_460_730_472_580_800L
val price = 19.99
val grade = 'B'
val passed = true
println(s"$small $count $distance")
println(s"$price $grade $passed")
println(Int.MaxValue)
println(Long.MaxValue)120 2000000000 9460730472580800
19.99 B true
2147483647
9223372036854775807
Numbers have methods
Because every value is an object, numbers have useful methods:
@main def run(): Unit =
println(-7.abs)
println(3.max(9))
println(2.5.round)
println(10.toDouble / 4)
println(65.toChar)
println('a'.toInt)
println(255.toHexString)
println("42".toInt + 1)7
9
3
2.5
A
97
ff
43
Integer division and overflow
Dividing two integers gives an integer: the fractional part is thrown away. And because Int has a fixed size, going past Int.MaxValue silently wraps around.
@main def run(): Unit =
println(7 / 2)
println(7 % 2)
println(7.0 / 2)
println(Int.MaxValue + 1)
println(Int.MaxValue.toLong + 1)
println(0.1 + 0.2)
println(BigDecimal("0.1") + BigDecimal("0.2"))3
1
3.5
-2147483648
2147483648
0.30000000000000004
0.3
Tip: Use
BigDecimalfor money.Doubleis a binary floating-point number and cannot represent most decimal fractions exactly.
Conversions
Scala never converts between number types in a way that might lose information without you asking. Widening (for example Int → Long or Int → Double) happens automatically; narrowing needs an explicit toX call.
| From | Method | Result |
|---|---|---|
3.99 (Double) |
.toInt |
3 (truncated) |
42 (Int) |
.toDouble |
42.0 |
"17" (String) |
.toInt |
17 |
"abc" (String) |
.toIntOption |
None (no exception) |
'7' (Char) |
.asDigit |
7 |
@main def run(): Unit =
val raw = List("12", "x", "30")
val parsed = raw.map(_.toIntOption)
println(parsed)
println(parsed.flatten.sum)List(Some(12), None, Some(30))
42
The type hierarchy
All types share a common root:
Any
┌───────┴────────┐
AnyVal AnyRef (java.lang.Object)
Int, Double, Boolean, String, List, your classes…
Char, Unit, … │
Null
└───────┬────────┘
NothingAnyis the supertype of everything.AnyValcovers the value types;AnyRefcovers reference types (objects).Nothingis a subtype of every type. An expression that throws an exception has typeNothing, which is whythrowcan appear in any branch.Unithas exactly one value, written().
@main def run(): Unit =
val things: List[Any] = List(1, "two", 3.0, 'c', true, ())
for t <- things do
println(s"$t is a ${t.getClass.getSimpleName}")1 is a Integer
two is a String
3.0 is a Double
c is a Character
true is a Boolean
() is a BoxedUnit
When a value type is stored as Any, the JVM boxes it into an object such as java.lang.Integer — that is what getClass shows here.
Test yourself
What does 7 / 2 evaluate to in Scala?
3.5343.0
Which type is a subtype of every other type?
AnyAnyRefUnitNothing
What does "abc".toIntOption return?
0None- A
NumberFormatException Some(0)