Easy Tutorial
❮ Partially Applied Functions Scala Sets ❯

Scala Data Types

Scala shares the same data types as Java. The table below lists the data types supported by Scala:

Data Type Description
Byte 8-bit signed two's complement integer. Value range is -128 to 127
Short 16-bit signed two's complement integer. Value range is -32768 to 32767
Int 32-bit signed two's complement integer. Value range is -2147483648 to 2147483647
Long 64-bit signed two's complement integer. Value range is -9223372036854775808 to 9223372036854775807
Float 32-bit, IEEE 754 standard single-precision floating-point number
Double 64-bit IEEE 754 standard double-precision floating-point number
Char 16-bit unsigned Unicode character, with a range of values from U+0000 to U+FFFF
String Sequence of characters
Boolean true or false
Unit Represents no value, equivalent to void in other languages. Used as the result type for methods that do not return any result. Unit has only one instance value, written as ().
Null null or null reference
Nothing The Nothing type is at the bottom of Scala's class hierarchy; it is a subtype of any other type.
Any Any is the superclass of all other classes
AnyRef The AnyRef class is the base class for all reference classes in Scala

The data types listed in the table above are objects, which means that Scala does not have the primitive types found in Java. In Scala, you can call methods on basic types such as numbers.

Scala Basic Literals

Scala is very simple and intuitive. We will now introduce Scala literals in detail.

Integer Literals

Integer literals are used for the Int type. If representing a Long, you can add an L or lowercase l suffix to the number:

0
035
21
0xFFFFFFFF
0777L

Floating-Point Literals

If a floating-point number has an f or F suffix, it represents a Float type; otherwise, it is a Double type. Examples are as follows:

0.0
1e30f
3.14159f
1.0e100
.1

Boolean Literals

Boolean literals are true and false.

Symbol Literals

Symbol literals are written as: '<identifier>, where <identifier> can be any letter or number identifier (note: cannot start with a number). This literal is mapped to an instance of the predefined class scala.Symbol.

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

Character Literals

In Scala, character variables are defined using single quotes ', as follows:

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

The ** represents an escape character, which can be followed by a number like **u0041 or fixed escape characters like \r\n.

String Literals

In Scala, string literals are defined using double quotes ", as follows:

"Hello,\nWorld!"
"tutorialpro.org official website: www.tutorialpro.org"

Representation of Multiline Strings

Multiline strings are represented by three double quotes as the delimiter, in the format: """ ... """.

Example:

val foo = """tutorialpro.org
www.tutorialpro.org
www.w3cschool.cc
www.runnoob.com
The above three addresses can be accessed"""

Null Value

The null value is of type scala.Null.

Scala.Null and scala.Nothing are special types that handle certain "boundary cases" of Scala's object-oriented type system in a unified way.

The Null class is the type of the null reference object, and it is a subclass of every reference class (classes that inherit from AnyRef). Null is not compatible with value types.

Scala Escape Characters

The table below lists common escape characters:

Escape Character Unicode Description
\b \u0008 Backspace (BS), moves the current position to the previous column
\t \u0009 Horizontal tab (HT) (jumps to the next TAB position)
\n \u000a New line (LF), moves the current position to the beginning of the next line
\f \u000c Form feed (FF), moves the current position to the beginning of the next page
\r \u000d Carriage return
❮ Partially Applied Functions Scala Sets ❯