
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Extending a Class
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Currying Functions
- Scala Collections
- Scala - Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - Type Hierarchy
Scala is a programming language that checks types at compile time. It supports both object-oriented and functional programming styles. A key part of Scala is its type system, which is used to define and use different types of data. To become good at Scala, you need to understand this type system. Because it is the foundation for many of the language advanced features. In this post, we will discuss type hierarchy in Scala.
The Root of All Types: Any
At the very top of the Scala type hierarchy is the Any type. It is the supertype of all types in Scala. Every type in Scala, whether it is a primitive type, user-defined class, or function, inherits from Any. Any the most general type. Any has two important subtypes: AnyVal and AnyRef.

Example
val anyValue: Any = "Hello, Scala!"
Value Types: AnyVal
AnyVal is the supertype of all value types. These are the types that represent primitive values. These are generally mapped to Java primitive types for performance reasons. Scala defines various value types, i.e., Int, Double, Boolean, Char, Unit, and others. These are important value types in Scala −
- Int − It represents 32-bit integer values.
- Double − It represents 64-bit floating point values.
- Boolean − It represents true or false.
- Char − It represents a 16-bit Unicode character.
- Unit − It represents no value, similar to void in Java.
For example,
val intValue: Int = 42 val doubleValue: Double = 3.14 val booleanValue: Boolean = true
Reference Types: AnyRef
AnyRef is the supertype of all reference types. These are analogous to Java Object class. Every user-defined class, object and array in Scala inherits from AnyRef. AnyRef corresponds to Java Object and provides methods like equals, hashCode and toString.
For example,
class Person(val name: String, val age: Int) val person: AnyRef = new Person("Alice", 30)
In Scala, String is also a reference type and inherits from AnyRef.
Bottom Types: Nothing and Null
There are two special types at the bottom of the Scala type hierarchy: Nothing and Null.
Nothing is a subtype of every other type. There are no instances of Nothing. It is used to signal abnormal termination (like throwing an exception). It returns type for methods that never return normally.
For example,
def fail(message: String): Nothing = throw new RuntimeException(message)
Null is a subtype of all reference types (AnyRef), but not of value types (AnyVal). It represents the absence of a value. It is similar to null in Java. However, its usage is generally discouraged in favor of safer alternatives.
For example,
val nullValue: String = null
Special Types: Unit and Option
Unit is a value type. It signifies the absence of a meaningful value and has only one instance, (). It is similar to void in Java but is a first-class value in Scala.
For example,
def printMessage(message: String): Unit = { println(message) }
Option is not part of the type hierarchy. But it is an important type used to represent optional values. An Option can either be Some(value) if there is a value, or None if there is no value. This is a safer alternative to using null.
For example,
val someValue: Option[Int] = Some(42) val noValue: Option[Int] = None