Difference Between asInstanceOf and (O:T) in Scala

Suraj P Feb 23, 2022
  1. Using asInstanceOf[T] in Scala
  2. Using (o:T) in Scala
Difference Between asInstanceOf and (O:T) in Scala

This article will learn the differences between asInstanceOf[T] and (o:T) in Scala.

Using asInstanceOf[T] in Scala

In Scala, asInstanceOf[T] is used for typecasting or converting an object’s type to another, such as int to float. Since this is entirely a run-time operation, run-time errors are possible.

There are two types of typecasting in Scala. These are the implicit and explicit typecasting.

The compiler itself carries out implicit typecasting. This conversion can be lossy; data may be lost in some cases.

For instance, the division of two int values that can return a non-integer value will be of the int type, leading to data loss.

Explicit type casting is a user-defined conversion. The typecasting of values can be done directly without any special method.

However, if we want to typecast references, we must use the asInstanceOf method.

Syntax: The type of object is changed to T.

object.asInstanceof[T]

The following code examples demonstrate explicit type casting using the asInstanceOf method.

Example 1:

object MyClass {
    //the method takes input int type and outputs char type
    def intTocharConverter(obj: Int): Char = {
        return obj.asInstanceOf[Char]
    }
    def main(args: Array[String]):Unit= {
        val obj = 100
        println("The value of obj is: " + obj + ", type of value is " + obj.getClass)

        val obj2 = intTocharConverter(obj) //ASCII value of 100 (d) is present in variable obj2
        println("Value after casting is: " + obj2 + ", type of value is " + obj2.getClass)
    }
}

Output:

The value of obj is: 100, type of value is int
Value after casting is: d, type of value is char

Example 2:

object MyClass {
    //the method takes input int type and outputs double type
    def intTodoubleConverter(obj: Int): Double = {
        return obj.asInstanceOf[Double]
    }
    def main(args: Array[String]):Unit= {
        val obj = 123
        println("The value of obj is: " + obj + ", type of value is " + obj.getClass)

        val obj2 = intTodoubleConverter(obj) 
        println("Value after casting is: " + obj2 + ", type of value is " + obj2.getClass)
    }
}

Output:

The value of obj is: 123, type of value is int
Value after casting is: 123.0, type of value is double

Using (o:T) in Scala

This type ascription is an up-cast done at compile-time for the sake of the type checker. Up-casting, also called widening, means we cast a subclass instance to one of its superclasses.

Since this is a compile-time operation, we may get compile-time errors, but no run-time error can occur. This type ascription can also be used for implicit conversions.

Below is an example of code for implicit conversion using ascription.

object MyClass {
    implicit def foo(s:String):Int = s.length

    def main(args: Array[String]):Unit= {
        val obj = "123"
        println(foo(obj))
    }
}

Output:

3

Let’s summarize the discussion in tabular form.

asInstanceOf[T] (o:T)
Used for explicit type conversion for references. Can be used for implicit type conversion.
Run-time operation. Compile-time operation.
Run-time errors may occur like ClassCastException. Compile-time errors may occur like type mismatch errors.
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub