Data Types in Scala
Like most programming languages out there, Scala has multiple data types.
Every data type is a class, making every variable an instance of a class.
Built-in Data Types
Let us look into some built-in data types available in Scala.
val x: Byte = 1; // Byteval x: Int = 12; // Integerval x: Long = 12; // Long Integerval x: Short = 12; // Short Integerval x: Double = 12.0; // Double Precision Numberval x: Float = 12.0; // Floating Point Number
Of these types,
Double are the default
numeric data types that do not need explicit annotation.
val x = 1; // Intval x = 1.0; // Double
You can also omit annotation for some other numeric data types by appending a certain character to the value itself.
val x = 7L; // Longval x = 7.0F; // Floatingval x = 7.0D; // Double
Characters and Strings
Characters are denoted by single quotes (eg.
'C') and strings are denoted
by double quotes (eg.
val x = "foo"; // Stringval x = 'f' // Char
Multiline strings can be created using three double quotes. However, stripping the indents is your job.
val x = """ Water Is Good """
The Unit Type
Unit is a type similar to
void in many other languages.
Union & Intersection
Union types are represented using
Type1 | Type2.
Intersection types are represented using
Type1 & Type2.