Register Login

Scala Type Hierarchy Overview

Updated May 18, 2018

Scala type hierarchy vs Java type hierarchy

In Scala, you can mention the method name without a dot operator.

When entering a list of numbers, it will return you a list of [int]

If you add a boolean value to the list, it returns an array of Any Value [AnyVal]

By comparison, in Java, while creating Arrays, we have to be specific about the items inserted. For eg, if we enter a boolean value with integers, it returns the list as 'Object'

Java Hierarchy

Scala Hierarchy 

As you can see, the topmost hierarchy is 'Any' instead of an object. Then 'Any' has two subdivisions 'AnyVal' and 'AnyRef'

When it comes to the primitive types to store data like numbers, characters or Boolean values, it is stored in 'AnyVal'. When you are talking about storing your own object, that is stored in 'AnyRef'.

Now, in Scala, if we add a string to the list, along with numbers and Boolean values, it will return [Any], which as per the hierarchy discussed above, covers the entire spectrum of values stored.

So that’s how the Scala type hierarchy goes and how it differs from Java type hierarchy.