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 CategoryData TypeDescriptionActual class in the Scala standard library
Basic typesByte8-bit signed integer, value range from -128 to 127scala.Byte
Basic typesShort16-bit signed integer, value range from -32768 to 32767scala.Short
Basic typesInt32-bit signed integer, value range from -2147483648 to 2147483647scala.Int
Basic typesLong64-bit signed integer, value range from -9223372036854775808 to 9223372036854775807scala.Long
Basic typesFloat32-bit IEEE 754 single-precision floating-point numberscala.Float
Basic typesDouble64-bit IEEE 754 double-precision floating-point numberscala.Double
Basic typesChar16-bit unsigned Unicode character, value range from U+0000 to U+FFFFscala.Char
Basic typesStringString type, representing a sequence of charactersjava.lang.String
Basic typesBooleanBoolean type, values aretrueorfalsescala.Boolean
Collection typesListImmutable linked listscala.collection.immutable.List
Collection typesSetImmutable setscala.collection.immutable.Set
Collection typesMapImmutable key-value pair collectionscala.collection.immutable.Map
Collection typesArrayMutable arrayscala.Array
Collection typesTupleImmutable container that can contain elements of different typesscala.TupleN
Collection typesOptionRepresents a container that may contain a value or be emptyscala.Option
Collection typesEitherRepresents one of two possible value typesscala.util.Either
Collection typesTryContainer for handling operation results that may succeed or failscala.util.Try
Special typesUnitRepresents no value, equivalent to Java'svoidscala.Unit
Special typesNullSingleton object representing the null value for all reference typesscala.Null
Special typesNothingRepresents no return value type, is a subtype of all typesscala.Nothing
Special typesAnySupertype of all typesscala.Any
Special typesAnyRefSupertype of all reference types, equivalent to Java'sObjectscala.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:

object DataTypeExamples {
  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: null
Other extensions