Scala Data Types by Code Example

Being a purely object oriented language each and every value in Scala world is an object. Even all of the primitive data types of Scala are the objects. And as I also has mentioned in my earlier post related to Scala Methods, there is no native operator available in Scala. Instead, Scala uses a corresponding method which look like an operator.

So basically when you are writing 2 + 3 it means you are calling + method of scala.Int class like 2.+(3).

Let’s have a look know on basic Scala data types.

Scala Data Types
Scala Data Types

As you can see from above diagram there are three top data types scala.Any, scala.AnyRef and scala.AnyVal

scala.Any

  • scala.Any is the top most data type of all classes in Scala. Any class in Scala is a subclass of scala.Any type
  • It contains methods like ==, !=, toString, equals, hashCode etc…

scala.AnyRef

  • scala.AnyRef is a super class of any reference types in Scala
  • All objects having type java.lang.Object as well as other non primitive objects in Scala are subtype of scala.AnyRef

scala.AnyVal

  • scala.AnyVal is a super class of all primitive classes
    Here is a list of all primitive classes

Here is a list of all primitive classes

TypeDescription
scala.Double64 bits
scala.Float32 bits
scala.Long64 bits
scala.Int32 bits
scala.Short16 bits
scala.Byte8 bits
scala.Char16 bits
scala.Booleantrue or false
scala.UnitMeans no value (can be compared with void in java)

As you can see there are two bottom classes scala.Null and scala.Nothing

scala.Null

  • In Scala Null is also an object. scala.Null corresponds to the null value or empty reference
  • It is a subclass of any scala.AnyRef type of object so it can be assigned to such references

scala.Nothing

No one can create such object like Nothing in Scala

Basically it is used to,

  • Give signal of abnormal termination, like while throwing an Error from the application
  • To denote empty Collection

Examples of Data Types,

Hope you nJoy reading this article…..!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *