×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

By IncludeHelp Last updated : October 28, 2024

Object Casting in Scala

By IncludeHelp Last updated : November 01, 2024

Object Casting

Object casting is changing the object's type from one type to another type. Instances or objects in Scala are casted using the asInstanceOf method in Scala.

asInstanceOf Method

The asInstanceOf method in Scala is used to cast the instance of one type into another type. It is a concrete method of Any class which is the root of Scala class hierarchy so it is by default inherited.

Syntax

def asInstance[objecttype] : objecttype

The method takes an objecttype as a parameter and returns the casted object.

Syntax

The syntax used for casting object using object casting using asInstanceOf method,

object.asInstanceof[type]

Example 1: Casting an object of numeric type (int) to non-numeric type (char)

object myObject {
 def flaotConvertor(a: Int): Char = {
  return a.asInstanceOf[Char]
 }

 def main(args: Array[String]) {
  val obj1 = 99
  println("Initial value is " + obj1 + ", type of value is " + obj1.getClass)
  val obj2 = flaotConvertor(obj1)
  println("Object casting Done!")
  println("Value after casting is " + obj2 + ", type of value is " + obj2.getClass)
 }
}

Output

Initial value is 99, type of value is int
Object casting Done!
Value after casting is c, type of value is char

Explanation

In the above code, we have created an instance of integer type and then cast it to another instance of type char. The output we got is the character converted for the integer value taking the integer value as ASCII value. For this, we have created a method named flaotConvertor() which took the object as a parameter and return the object of another type. In the function, we have used the asInstanceOf method. To find the type of object we have used the method getClass.

Example 2: Casting an object from one numeric type (int) to another numeric type (float)

object myObject {
 def flaotConvertor(a: Int): Float = {
  return a.asInstanceOf[Float]
 }

 def main(args: Array[String]) {
  val a = 10
  println("Initial value is " + a + ", type of value is " + a.getClass)
  val b = flaotConvertor(a)
  println("Object casting Done!")
  println("Value after casting is " + b + ", type of value is " + b.getClass)
 }
}

Output

Initial value is 10, type of value is int
Object casting Done!
Value after casting is 10.0, type of value is float

Explanation

In the above code, we have created an instance of integer type and then cast it to another instance of type float. For this, we have created a method named flaotConvertor() which took the object as a parameter and return the object of another type. In the function, we have used the asInstanceOf method. To find the type of object we have used the method getClass.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.