Scala Casting

In programming languages, we find a situation where we need the already existing variable defined in a data type to be converted into a different data type. For this Scala, typecasting is an approach that will change the data type of variable into other data types. But not every data type can be converted to another data type of your choice. In the article, we will discuss Scala typecasting for a better understanding of the conversion of data type to the other type.

What is Typecasting in Scala?

In typecasting, the word type tells the compiler about which type of data we have in a program. The type is also known as data type. Typecasting is the conversion of an already existing data type to another data type. This conversion from one type to another is possible but we cannot reverse them again, it is not possible in Scala due to the different sizes of data types. There might be a risk of data loss to convert a greater size of data type to a smaller one.

Syntax of the Typecasting in Scala

The general syntax of Scala type casting looks in this way:

object_name = object.asInstanceOf[class]

The asInstanceOf method is used to typecast. In the syntax above, object_name is defined in which casted instance of the object is returned, the object is the object to be cast. The Class is the name of a class in which an object is cast.

How to Type Casting in Scala in Ubuntu 20.04?

To know, how we can caste data type from one to another type in Scala, we should take a look at the examples:

Example # 1: Program of Implicit Casting in Scala Ubuntu 20.04

When the compiler typecasts any value or variable itself, then it is implicit typecasting. Data might be lost i.e., in a case where integer value returns a float value then the compiler only prints the integer type value which can lead to data loss. We have a code below to demonstrate how implicit casting can be done.

In the above example code, we have a class as “TypeConversion1”. The class has a main method in which we have created a value as “val” and define it as “myValue”. The variable “myValue” is initialized with the value “489” of the data type integer. After division, the result is saved in a new variable “myResult” which returns the remainder value. This division operation leaves a decimal point but due to implicit type conversion, we will get the integer value. Through print statements, we will have data types value with types mentioned.

The output has an integer value displayed in the image below but there should be a float value returned on the division operation. This is due to implicit type casting which loses decimal values.

Example # 2: Program of Explicit Casting in Scala in Ubuntu 20.04

The above example code has an issue with data loss. We can solve it by using explicit casting. The conversion of data type value or variable is defined by the user. This type of casting is user-defined casting in Scala.

In the above code, we have two types of conversion. The implementation is in a Scala class named “TypeConversion2”. The class main method defines a conversion of data type. First, we have created a variable represented as “x” of data type short that has a value “999”. Then, we added another variable “y” that converts the data type from short to long and initializes it with the short type. For this, we have a variable as “i” of type char that stores a character “Z”. We have created another variable “j” for converting type from char to float and initialize it with the value of char. Through the getClass method, we will get the data type of the specific values. The print function will return the output values of specified variables in it.

We can see the long value of “999” on the terminal screen and also the character “Z” value in the Float data type.

Example # 3: Program of using the asInstanceOf method in Scala Ubuntu 20.04

The asInstanceOf function is a special type casting technique in Scala. It takes a parameter of the value in which type is specified. The method primarily works on run time. If during the run time the type specified does not match the value on which the method is called, it throws an exception.

In the above code of the class declared as “TypeConversion3”, we have defined a variable as “p” of data type integers. The variable “p” stores a value “1999” on which typecasting will be done. Then, we have created a variable “Result1” of type float. This will convert type from Int to float. The print statement will return the value of “Result1” with the type mentioned. In the code, we have also taken character values for type conversion. We have created a variable “q” of data type char that stores a character value “M” and then we have initialized the value of character using the asInstanceOf method in a new variable “Result2” the data type will be in the ASCII value of character. The print statement will help to show the returned values of typecasting.

As shown in the image below, the double value of “1999” as an output and the ASCII value of character “M” as output from the typecast method.


The guide aims to give you a clear and concise understanding of Scala typecasting. We have covered almost everything about Scala typecasting. Firstly, we have a brief introduction to Scala typecasting. Then, we have general syntax and the example code implementation. In the example code, we have discussed two types of Scala typecasting: implicit casting and explicit casting. Also, we have gone through the Scala casting asInstanceOf method which can also be used for more complex applications, such as passing an array of the object instance. These examples will help you in dealing with typecasting cases.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.