Scala Tutorial Scala References

Scala - Literals



A literal (or literal data) is data that represents a fixed value in the source code, like the number 5, the character A, and the text "Hello, World". Scala has literals for integer numbers, floating point numbers, characters, booleans, symbols, and strings. The syntax of these literals in each case is same as in Java.

Integer Literals

Integer literals are usually of type Int, or of type Long when followed by a L or l suffix. Values of type Int are all integer numbers between −231 and 231−1, inclusive. Values of type Long are all integer numbers between −263 and 263−1, inclusive. A compile-time error occurs if an integer literal denotes a number outside these ranges.

The integral literal can be of type Byte, Short and Char. The numeric ranges given by these types are:

  • Byte: −27 to 27−1
  • Short: −215 to 215−1
  • Char: 0 to 216−1

Some common examples of integer literals are:

0
035
21
0xFFFFFFFF
-42L
0777L

Floating Point Literals

Floating point literals are of type Float when followed by a floating point type suffix F or f, and are of type Double otherwise. The type Float consists of all IEEE 754 32-bit single-precision binary floating point values, whereas the type Double consists of all IEEE 754 64-bit double-precision binary floating point values.

Some common examples of floating point literals are:

0.0
1e30f
3.14159f
1.0e-100
.1

Boolean Literals

The boolean literals true and false are members of type Boolean.

Character Literals

A character literal is a single character enclosed in quotes. The character is either a printable unicode character or is described by an escape sequence.

Please note that '\u000A' (line feed) is not a valid character literal because Unicode conversion is done before literal parsing and it is not a printable character. One can use instead the escape sequence '\n' or the octal escape '\12'.

Some common examples of character literals are:

'a'
'\u0041'
'\n'
'\t'

String Literals

A string literal is a sequence of characters in double quotes. The characters are either printable unicode character or are described by escape sequences. If the string literal contains a double quote character, it must be escaped, i.e. "\"". The value of a string literal is an instance of class String. Here are some string literals:

"Hello,\nWorld!"
"This string contains a \" character."

Multi-Line String Literals

A multi-line string literal is a sequence of characters enclosed in triple quotes """ ... """. The sequence of characters is arbitrary, except that it may contain three or more consecutive quote characters only at the very end. Characters must not necessarily be printable; newlines or other control characters are also permitted. Unicode escapes work as everywhere else, but none of the escape sequences here are interpreted. Below is the multi-line string literal:

"""the present string
spans 
four
lines."""

Escape Sequences

Escape SequencesUnicodeDescription
\b\u0008backspace BS
\t\u0009horizontal tab HT
\n\u000alinefeed LF
\f\u000cform feed FF
\r\u000dcarriage return CR
\"\u0022double quote "
\'\u0027single quote '
\\\u005cbackslash \

Symbol Literals

A symbol literal 'x is a shorthand for the expression scala.Symbol("x"). Symbol is a case class, which is defined as follows:

package scala
final case class Symbol private (name: String) {
  override def toString: String = "'" + name
}