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 class in the Scala standard library |
|---|---|---|---|
| Basic types | Byte | 8-bit signed integer, value range from -128 to 127 | scala.Byte |
| Basic types | Short | 16-bit signed integer, value range from -32768 to 32767 | scala.Short |
| Basic types | Int | 32-bit signed integer, value range from -2147483648 to 2147483647 | scala.Int |
| Basic types | Long | 64-bit signed integer, 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, 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, values aretrueorfalse | scala.Boolean |
| Collection types | List | Immutable linked list | scala.collection.immutable.List |
| Collection types | Set | Immutable set | scala.collection.immutable.Set |
| Collection types | Map | Immutable key-value pair collection | scala.collection.immutable.Map |
| Collection types | Array | Mutable array | scala.Array |
| Collection types | Tuple | Immutable container that can contain elements of different types | scala.TupleN |
| Collection types | Option | Represents a container that may contain 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 Java'svoid | scala.Unit |
| Special types | Null | Singleton object representing the null value for all reference types | scala.Null |
| Special types | Nothing | Represents no return value type, is a subtype of all types | scala.Nothing |
| Special types | Any | Supertype of all types | scala.Any |
| Special types | AnyRef | Supertype of all reference types, equivalent to Java'sObject | scala.AnyRef |
In Scala, all data types are objects.
Scala does not have the concept of primitive types like 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.
The following is a complete Scala program demonstrating the use of various data types:
DataTypeExamples.scala file code:
def main(args: Array[String]): Unit = {
// Basic types
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: nullOther extensions