Scala Data Types
The main data types supported by Scala include basic types, collection types, and special types.
The following table lists the data types supported by Scala:
| Type Category | Data Type | Description | Actual Classes in the Scala Standard Library |
|---|---|---|---|
| Basic types | Byte |
8-bit signed integer, with a value range from -128 to 127 | scala.Byte |
| Basic types | Short |
16-bit signed integer, with a value range from -32768 to 32767 | scala.Short |
| Basic types | Int |
32-bit signed integer, with a value range from -2147483648 to 2147483647 | scala.Int |
| Basic types | Long |
64-bit signed integer, with a value range from -9223372036854775808 to 9223372036854775807 | scala.Long |
| Basic types | Float |
32-bit IEEE 754 single-precision floating-point number | scala.Float |
| Basic types | Double |
64-bit IEEE 754 double-precision floating-point number | scala.Double |
| Basic types | Char |
16-bit unsigned Unicode character, with a value range from U+0000 to U+FFFF | scala.Char |
| Basic types | String |
String type, representing a sequence of characters | java.lang.String |
| Basic types | Boolean |
Boolean type, with values true or false |
scala.Boolean |
| Collection Types | List |
Immutable linked list | scala.collection.immutable.List |
| Collection Types | Set |
Immutable Collection | scala.collection.immutable.Set |
| Collection Types | Map |
Immutable collection of key-value pairs | scala.collection.immutable.Map |
| Collection Types | Array |
Mutable array | scala.Array |
| Collection Types | Tuple |
Immutable container that can hold elements of different types | scala.TupleN |
| Collection Types | Option |
Container that may hold a value or be empty | scala.Option |
| Collection Types | Either |
Represents one of two possible value types | scala.util.Either |
| Collection Types | Try |
Container for handling operation results that may succeed or fail | scala.util.Try |
| Special types | Unit |
Represents no value, equivalent to void in Java void |
scala.Unit |
| Special types | Null |
Singleton object representing the null value of all reference types | scala.Null |
| Special types | Nothing |
Represents the type with no return value, a subtype of all types | scala.Nothing |
| Special types | Any |
The supertype of all types | scala.Any |
| Special types | AnyRef |
The supertype of all reference types, equivalent to Object in Java Object |
scala.AnyRef |
In Scala, all data types are objects.
Scala has no concept like the primitive types in Java. Although Scala's basic data types (such as Int, Double, etc.) look syntactically similar to Java's primitive types, they are actually objects. This means you can call methods on these types.
For example, the Int type in Scala is actually an instance of the scala.Int class, and scala.Int is a final class that inherits from AnyVal. AnyVal is a special class in Scala that represents value types.
Below is a complete Scala program demonstrating the use of various data types:
DataTypeExamples.scala file code:
object
DataTypeExamples
{
def
main
(
args
:
Array
[
String
]
)
:
Unit
=
{
// Primitive type
val
byteValue
:
Byte
=
127
val
shortValue
:
Short
=
32767
val
intValue
:
Int
=
2147483647
val
longValue
:
Long
=
9223372036854775807L
val
floatValue
:
Float
=
3.14f
val
doubleValue
:
Double
=
3.141592653589793
val
charValue
:
Char
=
'A'
val
stringValue
:
String
=
"Hello, Scala!"
val
booleanValue
:
Boolean
=
true
// collection types
val
listValue
:
List
[
Int
]
=
List
(
1
,
2
,
3
)
val
setValue
:
Set
[
String
]
=
Set
(
"Scala"
,
"Java"
,
"Python"
)
val
mapValue
:
Map
[
String, Int
]
=
Map
(
"one"
1
,
"two"
2
,
"three"
3
)
val
arrayValue
:
Array
[
Int
]
=
Array
(
4
,
5
,
6
)
val
tupleValue
:
(
Int, String, Boolean
)
=
(
42
,
"Answer"
,
true
)
val
optionValue
:
Option
[
String
]
=
Some
(
"I am here"
)
val
eitherValue
:
Either
[
String, Int
]
=
Right
(
42
)
val
tryValue
:
Try
[
Int
]
=
Try
(
10
/
2
)
// special types
val
unitValue
:
Unit
=
(
)
val
nullValue
:
String
=
null
val
nothingValue
:
Nothing
=
throw
new
RuntimeException
(
"Nothing value"
)
// output all values
println
(
s
"Byte Value: $byteValue"
)
println
(
s
"Short Value: $shortValue"
)
println
(
s
"Int Value: $intValue"
)
println
(
s
"Long Value: $longValue"
)
println
(
s
"Float Value: $floatValue"
)
println
(
s
"Double Value: $doubleValue"
)
println
(
s
"Char Value: $charValue"
)
println
(
s
"String Value: $stringValue"
)
println
(
s
"Boolean Value: $booleanValue"
)
println
(
s
"List Value: $listValue"
)
println
(
s
"Set Value: $setValue"
)
println
(
s
"Map Value: $mapValue"
)
println
(
s
"Array Value: ${arrayValue.mkString("
,
")}"
)
println
(
s
"Tuple Value: $tupleValue"
)
println
(
s
"Option Value: $optionValue"
)
println
(
s
"Either Value: $eitherValue"
)
println
(
s
"Try Value: $tryValue"
)
println
(
s
"Unit Value: $unitValue"
)
println
(
s
"Null Value: $nullValue"
)
// nothingValue is not printed because it throws an exception
}
}
Compile with the scalac compiler and run with the scala command:
scalac DataTypeExamples.scala
scala DataTypeExamples
Output result:
Byte Value: 127
Short Value: 32767
Int Value: 2147483647
Long Value: 9223372036854775807
Float Value: 3.14
Double Value: 3.141592653589793
Char Value: A
String Value: Hello, Scala!
Boolean Value: true
List Value: List(1, 2, 3)
Set Value: Set(Scala, Java, Python)
Map Value: Map(one -> 1, two -> 2, three -> 3)
Array Value: 4, 5, 6
Tuple Value: (42,Answer,true)
Option Value: Some(I am here)
Either Value: Right(42)
Try Value: Success(5)
Unit Value: ()
Null Value: null