Skip to content

Scala Data Types

Scala has a rich type system where every value is an object. Unlike Java’s distinction between primitive types and object types, Scala unifies these concepts. Basic data types like Int, Double, Boolean are classes, inheriting from AnyVal.

These correspond roughly to Java’s primitives but are classes in Scala. They offer performance benefits as the runtime can often optimize them to underlying primitives.

Data TypeDescription
Byte8-bit signed integer. Range: -128 to 127
Short16-bit signed integer. Range: -32,768 to 32,767
Int32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647
Long64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use L suffix (e.g., 100L).
Float32-bit IEEE 754 single-precision floating-point. Use F suffix (e.g., 3.14F).
Double64-bit IEEE 754 double-precision floating-point. Default for decimal literals (e.g., 3.14).
Char16-bit unsigned Unicode character. Range: U+0000 to U+FFFF. Enclosed in single quotes (e.g., 'A').
BooleanRepresents logical values: true or false.
UnitRepresents non-returning computations (like void). Has only one value: (). Inherits from AnyVal.

These correspond to Java’s object types. All user-defined classes, traits, and standard library classes like String, List, Option inherit from AnyRef (which is an alias for java.lang.Object).

Common Reference TypeDescription
StringA sequence of characters. Backed by java.lang.String. Immutable.
All user-defined classesInstances of classes you define (e.g., class Person(...)).
Standard CollectionsList, Vector, Map, Set, Option, etc.
NullA type whose only instance is null. It’s a subtype of all AnyRef types. Avoid using null; prefer Option.
NothingA trait that is a subtype of every other type, including AnyVal. It has no instances. Useful for signaling abnormal termination (e.g., throw exception) or for empty collections (List[Nothing]).

All types inherit from a common root class Any. Any has two direct subclasses: AnyVal (for value types) and AnyRef (for reference types).

  • Any
    • AnyVal
      • Double, Float, Long, Int, Short, Byte, Char, Unit, Boolean
    • AnyRef (alias for java.lang.Object)
      • String, List, Option, All user classes, etc.
      • Null (subtype of all AnyRef)
  • Nothing (subtype of everything, including AnyVal and Null)

Literals are fixed values represented directly in code.

Default to Int. Use L or l suffix for Long.

val intLiteral = 42
val longLiteral = 1234567890L
val hexLiteral = 0x2A // Hexadecimal (equals 42)
val octalLiteral = 052 // Octal (equals 42) - Less common

Default to Double. Use F or f suffix for Float.

val doubleLiteral = 3.14159
val anotherDouble = 1.0e20 // Scientific notation
val floatLiteral = 2.718F

Simply true and false.

val isScalaFun = true
val isComplex = false

Single character enclosed in single quotes.

val charA = 'A'
val newlineChar = '\n' // Escape sequence
val unicodeChar = '\u0041' // Unicode for 'A'

Sequence of characters in double quotes.

val greeting = "Hello, world!\n" // Standard string with escape
// Multi-line strings using triple quotes
val multiLine = """This string
spans multiple
lines."""
// Multi-line strings with margin stripping
val indented = """|SELECT
| name,
| age
|FROM users
|WHERE active = true""".stripMargin

Deprecated in Scala 2.13 and removed in Scala 3. Symbols were used for identifiers that could be created dynamically, often in reflection or specific DSLs. Use strings instead.

// 'mySymbol // Deprecated/Removed syntax

The null literal belongs to the type Null and can be assigned to any AnyRef type. However, its use is discouraged in idiomatic Scala. Prefer Option[T] to represent the potential absence of a value.

val name: String = null // Possible, but discouraged
val age: Option[Int] = None // Preferred way for absence

Standard escape sequences can be used within Char and String literals:

SequenceDescription
\bBackspace
\tTab
\nNewline
\fForm feed
\rCarriage return
”Double quote
’Single quote
\Backslash

Unicode characters can also be represented using \uxxxx.