Learn The Scala Class And Type Hierarchy

By Nadim Bahadoor | Last updated: June 6, 2018 at 12:19 pm

Overview

In this tutorial, we will have a closer look at Scala's type hierarchy. To help you visualize the type hierarchy in Scala, you can refer to the diagram below which is a snapshot from Scala's documentation on Unified Types.

Scala Unified Types

The diagram shows that the type Any is at the top most of the Scala's class hierarchy.

 

If you are coming from Java or .NET, you can think of the Any type as the Object class. In other words, Any is the root type and it has two sub-classes namely AnyVal and AnyRef as per the above diagram.

 

So where are the built-in primitive types such as the ones we have in Java? The answer is simple, there are none!

Steps

1. Declare a variable of type Any

If you recall from the previous Type Inference tutorial, Scala is able to infer types.

But in this example let's type our immutable variable favoriteDonut as Any.


println("Step 1: Declare a variable of type Any")
val favoriteDonut: Any = "Glazed Donut"
println(s"favoriteDonut of type Any = $favoriteDonut")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: Declare a variable of type Any
favoriteDonut of type Any = Glazed Donut

2. Declare a variable of type AnyRef

Let's declare another variable called donutName. But this time you will use the type AnyRef.


println("\nStep 2: Declare a variable of type AnyRef")
val donutName: AnyRef = "Glazed Donut"
println(s"donutName of type AnyRef = $donutName")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: Declare a variable of type AnyRef
donutName of type AnyRef = Glazed Donut

3. Declare a variable of type AnyVal

Let's now declare another variable donutPrice to be of type AnyVal and assign its value to 2.50.


println("\nStep 3: Declare a variable of type AnyVal")
val donutPrice: AnyVal = 2.50
println(s"donutPrice of type AnyVal = $donutPrice")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: Declare a variable of type AnyVal
AnyVal donutPrice = 2.5

This concludes our tutorial on Learn The Scala Class And Type Hierarchy and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to declare a variable of type Any
  • How to declare a variable of type AnyRef
  • How to declare a variable of type AnyVal

Tip

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over Enumerations in Scala.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: