Solution Review: Type Casting
Explore how to cast variables from Float to Double in Scala. This lesson helps you understand declaring variables with specific types and assigning values through type casting for accurate data manipulation.
Task
In this challenge, you were provided with a variable that was previously defined to have a data type Float and you were asked to type cast it to a Double.
Solution
Let’s look at how we should go about this.
-
The first thing is deciding the variable name, which you were told to keep as
newType. -
As type casting is from
FloattoDoublewhen definingnewTypespecify the data type asDouble. -
The last step is simply assigning
newTypethe value ofoldType.
val newType: Double = oldType
You can find the complete solution below:
You were required to write the code on line 2.
This brings our discussion on basic data types to an end. In the next lesson, we will move on to strings.