Since Scala has some similarity with Java, it is no surprise that it has the same data types as in Java. Although there are a few additional data types.
We cover the following sub-topics:
1. Scala Data Types
The table below gives a list of Scala data types.
| Data Type | Brief Description |
|---|---|
| Byte | 8 bit signed value. Range from -128 to 127 |
| Short | 16 bit signed value. Range -32768 to 32767 |
| Int | 32 bit signed value. Range -2147483648 to 2147483647 |
| Long | 64 bit signed value. -9223372036854775808 to 9223372036854775807 |
| Float | 32 bit IEEE 754 single-precision float |
| Double | 64 bit IEEE 754 double-precision float |
| Char | 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF |
| String | A sequence of Chars |
| Boolean | Either the literal true or the literal false |
| Unit | Corresponds to no value |
| Null | null or empty reference |
| Nothing | The subtype of every other type; includes no values |
| Any | The supertype of any type; any object is of type Any |
| AnyRef | The supertype of any reference type |
Just like in Python, all data types in Scala are object data types
It is also good to know that that the data types in Scala are objects. Also called reference types or non-primitive types. This means that the
2. Literals in Scala
Literals are used to define a constant value for example ‘Kindson’, 0.023, 120.
Let’s take a look at some of the basic literals in Scala programming language.
Int Literals – These are values without a decimal point. I could be of type Long when suffixed by L or l. Otherwise it is an Int
Floating Point Literals – These are floating point values which maybe sometimes followed by the suffix F or f. When followed by a suffix, it is a Float, otherwise it is a double.
Boolean Literals – Boolean literals are true or false values
Symbol Literals – A symbol literal ‘x’ in scala represents the expression scala.Symbol(“x”)
Character Literals – Character literals in Scala is enclosed in single quotes. For example ‘A’, ‘2’. The character could also be an escape sequence (‘\n’ for example) or unicode characters (‘\u0041’) for example.
String Literals – Just like in Java, String literals in Scala is enclosed in double quotes.
Multi-line Strings – A multi-line string in Scala begins with tripple double quotes (“””) and ends with same. An example is given below:
"""this is example of multi-line string in scala programming."""
Null Values – Null value is scala is of type scala.Null. It represents a reference to a special “null” object
3. Escape Sequences
An escape sequence is a sequence of characters that when used within a string, translates to another character or sequence which would otherwise be difficult to represent directly. For instance, ‘\n’ translates a newline.
The table below provides a list of escape sequences in Scala.
| Escape Sequences | Unicode | Description |
|---|---|---|
| \n | \u000c | newline NL (Form feed) |
| \t | \u0009 | horizontal tab HT |
| \b | \u0008 | backspace BS |
| \f | \u000c | formfeed FF |
| \r | \u000d | carriage return CR |
| \” | \u0022 | double quote “ |
| \’ | \u0027 | single quote . |
| \\ | \u005c | backslash \ |