There are no primitive types in Scala(unlike Java). All data types in Scala are objects that have methods to operate on their data. All of Scala's types exist as part of a type hierarchy. Every class that we define in Scala will also belong to this hierarchy automatically.

Any
Any is the superclass of all classes, also called the top class. It defines certain universal methods such as equals, hashCode, and toString. Any has two direct subclasses:
- AnyVal
- AnyRef
Example:
Scala
// Scala program of Scala Type hierarchy
// Creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
val list: List[Any] = List(
false,
66677,
732,
'a',
"abs"
)
list.foreach(element => println(element))
}
}
Outputfalse
66677
732
a
abs
AnyVal
AnyVal represents value classes. All value classes are predefined; they correspond to the primitive types of Java-like languages.
- There are nine predefined value types and they are non-null able: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean.
- Scala has both numeric (e.g., Int and Double) and non-numeric types (e.g., String) that can be used to define values and variables.
- Boolean variables can only be true or false. Char literals are written with single-quotes.
- These are not nullable except Unit and all value classes are an abstract final type.
Example:
Scala
// Scala program of Scala Type hierarchy
// Using AnyVal.
// Creating Object
object Geeks
{
// Main method
def main(args: Array[String])
{
val list: List[AnyVal] = List(
333, true, false
)
list.foreach(element => println(element))
}
}
Exception
Scala's value types are much like Java's primitive types except the Unit class.
- Unit is a value type that does not carry any real information.
- Unit class similar to Java’s Void type.
- There exists a single instance of Unit that may be expressed literally in this manner: ().
- Sometimes, functions need to return something, even nothing, so Unit comes in handy there.
AnyRef
AnyRef represents reference classes. All non-value types are defined as reference types.
- In Scala, AnyRef is the root class of all reference types and child of “Any” class in the Scala type system.
- These are types that refer to objects rather than directly holding values.
- Every custom type in Scala belongs to AnyRef category.
- User-defined classes define reference types by default; i.e. they always (indirectly) subclass scala.
- AnyRef. scala.AnyRef in java programming corresponds to java.lang.Object.
Example:
Scala
// Scala program of Scala Type hierarchy
// Using AnyRef
// Creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
val list: List[AnyRef] = List(
"GFG", "GEEKSFORGEEKS"
)
list.foreach(element => println(element))
}
}
Nothing and Null
Nothing
Nothing is a subclassify of all value types, it is also called the bottom type. Type Nothing That has no value.
- In Nothing you can't create an instance of it or assign it to any variable.
- It has zero instances.
- One of the reasons to use Nothing is to provide a return type for the functions that never return i.e. a method which always throws an exception.
- Nothing used as a return type for the operations that doesn’t return a value.
- We can use Nothing to signal non-termination such as a thrown exception, program exit, or an infinite loop .
Null
Null is a subclassify of all reference types. the keyword literal null can identify a single value.
- Null class is a child of all reference classes. But generally speaking, you should try to avoid using Null in typical Scala programming.
- Using Scala's literal keyword “null”, you can assign a Null to any reference class.
- Null is provided mostly for interoperability with other JVM languages.
Similar Reads
Scala Any type In Scala, the Any class is at the top of the hierarchy of classes. Scala stands like the supertype of all types and provides a foundation for Scala's type system. Any is like a supertype from which all values in scala are inherited whether it is a value type like(Int, Double, Float, etc.) or a refer
7 min read
Scala AnyRef type The Scala kind hierarchy starts with Any, that's the supertype of all types. The direct subclasses of Any are AnyVal and AnyRef. Whereas AnyVal represents price kinds (which includes Int, Double, and so on.), AnyRef represents reference types. AnyRef serves because the fundamental type and root deta
3 min read
Scala Path Dependent Type Scala Path Dependent Types (PDTs) are an advanced feature of the Scala language that allows users to create types that are dependent on the path in which they are accessed. The type of a variable or object is not determined by its own structure or characteristics, but rather by the path in which it
4 min read
Higher-Kinded Types in Scala This article focuses on discussing Higher-Kinded types in Scala. What is the Higher-Kinded Type? A higher-kinded type is a type that can stand for other types, which themselves stand for more types. It's a way to talk about types that are built from other types. They let us write code that can handl
4 min read
How to use java library in Scala? As Scala and Java are meant to work together effortlessly, using Java libraries in Scala programming is a breeze. To utilize a Java library in Scala, follow these steps: Importing Java Classes: Use Scala's import statement to import the required Java classes or packages.Creating Java Objects: Use Sc
2 min read