Overview of Scala Data Types
Scala data types are inherent to all the programming languages. They play a crucial role in defining all the operations that can be performed in the language. I won’t be incorrect if I say that the language paradigm is decided by the type system in the language.
An excerpt from Pierce, Benjamin C. (2002). Types and Programming Languages. MIT Press. ISBN 978-0-262-16209-8. “In programming languages, a type system is a set of rules that assigns a property called type to the various constructs of a computer program, such as variables, expressions, functions or modules.”
Type Hierarchy in Scala Data Type
Scala is a multi-paradigm programming language and has an elegant type system. But this article is not about the type system. In this article, we are going to look at the properties in the type system, the Unified Types in Scala. So, let’s get started.
The above figure shows in detail the type Hierarchy in scala. Any type is the parent type for all the types and Nothing extends all the types in the graph. These are called top-type and bottom-type. Let us look at each of them before diving into other types.
Top Type and Bottom Type
Following are the different types of hierarchy explained in detail.
Any is the super-type for all the types in scala. Some of the universal methods such as equals, toString, and hashCode are defined here. Then on the types are classified into two main categories,
- Predefined value types (AnyVal)
- Reference types (AnyRef)
1. Predefined Value Types: AnyVal extends the Any type to support all the predefined value types. Value types in AnyVal are non-nullable. Let us take a look at each one of them with examples.
- Int: Integer
abstract final class Int extends AnyVal
Int is a 32 bit signed integer type which is equivalent to Java’s primitive type int. The range of values allowed in Int is -2^31 to 2^31-1.
var intVar = 0; // Type inference
var intVar : Int = 0;
- Char: Character
abstract final class Char extends AnyVal
Char is a 16-bit unsigned integer which is equivalent to Java’s primitive type char. The range of values allowed in Char is 0 to 2^16-1.
var charVar = ‘A’; // Type inference
var charVar : Char = ‘A’;
abstract final class Long extends AnyVal
Long is a 64 bit signed integer which is equivalent to Java’s primitive type long. The range of values allowed in Char is -2^64-1to 2^64-1.
var longVar : Long = 86400000 * 150;
abstract final class Float extends AnyVal
Float is a 32-bit IEEE-754 single-precision floating-point number which is equivalent to Java’s primitive type float.
var floatVar = 1.23; // Type inference
var floatVar : Float = 21.43;
abstract final class Double extends AnyVal
Double is a 64-bit IEEE-754 double-precision floating-point number which is equivalent to Java’s primitive type double.
var doubleVar = 86400000 * 150.23; // Type inference
var doubleVar : Double = 86400000 * 150.23;
abstract final class Byte extends AnyVal
A byte is an 8 bit signed integer which is equivalent to Java’s primitive type byte.
var byteVar : Byte = 121;
abstract final class Short extends AnyVal
Short is a 16 bit signed integer which is equivalent to Java’s primitive type short.
var shortVar : Short = 23;
abstract final class Boolean extends AnyVal
Boolean is equivalent to Java’s primitive type boolean. The range of values allowed in Boolean is true and false.
var booleanVar = true; // Type inference
var booleanVar : Boolean = true;
abstract final class Unit extends AnyVal
Unit is a subtype of AnyVal and there is only one value that can be assigned to a Unit, (). The unit is similar to void in java.
2. Reference Types: All the reference types in scala extend AnyRef. AnyRef is similar to java.lang. object in the context of Java. All the user-defined types and non-value types extend AnyRef. Some of the examples of reference types are List, Option.
Nothing is a subtype of all types in scala. One use case of Nothing is as a return type for functions that do not return normally. It is also used when you are developing the outline of a feature without consideration of the implementation details. In those cases you use?
One of the most confusing statements found in the scala document says there is no such value that has type as Nothing. Why so? I will leave this exploration for you. On a similar note, Null is a subtype of all the AnyRef types. It is added as a part of the type system to offer compatibility with JVM languages. It has only one value, null.
Casting usually happens when there is a narrow type and a broader type. In this case, the narrow type increases its width to make room for the broader type. The below figure diagrammatically shows the direction of the type of conversions.
Literals in Scala
Literals are a way to denote or represent a fixed value in the source code. Below we have listed basic literals in Scala with examples.
- It can be of type int, long
- They are suffixed with I and L
- Eg: 14, 53FFF
2. Floating Point
- Can be float
- Suffixed by F
- Eg: 4.21
- Set of characters inside double quotes
- Eg: “EduCBA”
- Single char enclosed in quotes
- Eg: ‘/n’
- Chars that are enclosed in triple quotes “<chars>”
- Eg: “““This is an example of a Multiline literal”””
- Value of AnyRef Null
- Eg: null
- Eg: true, false
- Members of type Boolean
8. Escape Sequences
- Eg: \n, \t
This is a guide to Scala Data Types. Here we discuss the introduction and type hierarchy in scala data type along with type casting and literals in scala. You may also look at the following articles to learn more –