Work in Progress

Scala 3 has not been released, yet. We are still in the process of writing the documentation for Scala 3. You can help us to improve the documentation.

Are you searching for the Scala 2 documentation?

Scala 3 — Book

Variables and Data Types


This section provides a look at Scala variables and data types.

Two types of variables

When you create a new variable in Scala, you declare whether the variable is immutable or mutable:

Variable Type Description
val Creates an immutable variable—like final in Java. You should always create a variable with val, unless there’s a reason you need a mutable variable.
var Creates a mutable variable, and should only be used when a variable’s contents will change over time.

These examples show how to create val and var variables:

// immutable
val a = 0

// mutable
var b = 1

In an application, a val can’t be reassigned. You’ll cause a compiler error if you try to reassign one:

val msg = "Hello, world"
msg = "Aloha"   // "reassignment to val" error; this won’t compile

Conversely, a var can be reassigned:

var msg = "Hello, world"
msg = "Aloha"   // this compiles because a var can be reassigned

Declaring variable types

When you create a variable you can explicitly declare its type, or let the compiler infer the type:

val x: Int = 1   // explicit
val x = 1        // implicit; the compiler infers the type

The second form is known as type inference, and it’s a great way to help keep this type of code concise. The Scala compiler can usually infer the data type for you, as shown in the output of these REPL examples:

scala> val x = 1
val x: Int = 1

scala> val s = "a string"
val s: String = a string

scala> val nums = List(1, 2, 3)
val nums: List[Int] = List(1, 2, 3)

You can always explicitly declare a variable’s type if you prefer, but in simple assignments like these it isn’t necessary:

val x: Int = 1
val s: String = "a string"
val p: Person = Person("Richard")

Notice that with this approach, the code feels more verbose than necessary.

Built-in data types

Scala comes with the standard numeric data types you’d expect, and they’re all full-blown instances of classes. In Scala, everything is an object.

These examples show how to declare variables of the numeric types:

val b: Byte = 1
val i: Int = 1
val l: Long = 1
val s: Short = 1
val d: Double = 2.0
val f: Float = 3.0

Because Int and Double are the default numeric types, you typically create them without explicitly declaring the data type:

val i = 123   // defaults to Int
val j = 1.0   // defaults to Double

In your code you can also append the characters L, D, and F (and their lowercase equivalents) to numbers to specify that they are Long, Double, or Float values:

val x = 1_000L   // val x: Long = 1000
val y = 2.2D     // val y: Double = 2.2
val z = 3.3F     // val z: Float = 3.3

When you need really large numbers, use the BigInt and BigDecimal types:

var a = BigInt(1_234_567_890_987_654_321L)
var b = BigDecimal(123_456.789)

Where Double and Float are approximate decimal numbers, BigDecimal is used for precise arithmetic.

Scala also has String and Char data types:

val name = "Bill"   // String
val c = 'a'         // Char


Scala strings are similar to Java strings, but they have two great additional features:

  • They support string interpolation
  • It’s easy to create multiline strings

String interpolation

String interpolation provides a very readable way to use variables inside strings. For instance, given these three variables:

val firstName = "John"
val mi = 'C'
val lastName = "Doe"

You can combine those variables in a string like this:

println(s"Name: $firstName $mi $lastName")   // "Name: John C Doe"

Just precede the string with the letter s, and then put a $ symbol before your variable names inside the string.

To embed arbitrary expressions inside a string, enclose them in curly braces:

println(s"2 + 2 = ${2 + 2}")   // prints "2 + 2 = 4"

val x = -1
println(s"x.abs = ${x.abs}")   // prints "x.abs = 1"

The s that you place before the string is just one possible interpolator. If you use an f instead of an s, you can use printf-style formatting syntax in the string. Furthermore, a string interpolator is a just special method and it is possible to define your own. For instance, some database libraries define the very powerful sql interpolator.

Multiline strings

Multiline strings are created by including the string inside three double-quotes:

val quote = """The essence of Scala:
               Fusion of functional and object-oriented
               programming in a typed setting."""

For more details on string interpolators and multiline strings, see the “First Look at Types” chapter.

Contributors to this page: