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 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 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 is a super class of all primitive classes
    Here is a list of all primitive classes

Here is a list of all primitive classes

Type Description
scala.Double 64 bits
scala.Float 32 bits
scala.Long 64 bits
scala.Int 32 bits
scala.Short 16 bits
scala.Byte 8 bits
scala.Char 16 bits
scala.Boolean true or false
scala.Unit Means no value (can be compared with void in java)

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

  • 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

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,

//With type/return type scala.Any
scala> var anyVariable: Any = 1
anyVariable: Any = 1

scala> var anyVariable2 = List(1,2,3)
anyVariable2: Any = List(1, 2, 3)

scala> def getAny() = if(true) 1 else List(1,2,3)
getAny: ()Any

//With type/return type scala.AnyRef
scala> var anyRefVariable:AnyRef = new java.util.Date
anyRefVariable: AnyRef = Fri Jul 03 23:47:23 IST 2015

scala> def getAnyRef() = if(true) new java.util.Date else new java.lang.StringBuilder
getAnyRef: ()

//With type/return type scala.AnyVal
scala> var anyValVariable:AnyVal = 1
anyValVariable: AnyVal = 1

scala> def getAnyVal() = if(true) 1 else false
getAnyVal: ()AnyVal

//With return type scala.Unit
scala> def printMessage = println("Hi...!!!! How are You?????")
printMessage: Unit

//With type scala.Null
scala> var nullVariable = null
nullVariable: Null = null

//With return type scala.Nothing
scala> def error() = throw new Error()
error: ()Nothing

scala> var emptyList = List()
emptyList: List[Nothing] = List() //This is List of Nothing

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>