Topic Overview and Actualization

Welcome back! In today's lecture, we will delve into a critical concept within Scala: Primitive Data Type Conversion.

In software development, there can be scenarios where you need to manipulate data received in one format, typically from user inputs, and store or process it in a different format. For such occasions, it is essential to understand how to convert data types within Scala efficiently and safely.

By the end of this lesson, you will have learned the tools and techniques Scala provides for data type conversion. Additionally, you'll be able to perform these conversions safely while handling common errors efficiently. Let's get started!

Introduction to Data Type Conversion

Variables are used for storing and manipulating data in programming. Each variable has a specific data type that determines the kind of values it can hold. Sometimes, we need to move data across different types, and that's when data type conversion becomes essential.

Data type conversion, also known as typecasting, is the method of converting an entity from one data type to another.

Scala offers two broad categories of data type conversion:

  • Implicit Conversion: Scala automatically performs this type of conversion when a smaller type is assigned to a more sizeable type size, albeit with a few exceptions.

  • Explicit Conversion: As the name suggests, this type of conversion requires manual intervention and is frequently used when converting a larger data type to a smaller one.

Here's an example:

@main def run: Unit =
  val myNumber = 5    // Declares a variable myNumber and initializes it with an integer value 5.
  println(myNumber)   // Prints: 5
  val myDouble = myNumber.toDouble    // Converts to Double
  println(myDouble)   // Prints: 5.0

In this example, we declare a variable myNumber of type Int and value 5. We then explicitly convert it to a Double, resulting in myDouble. This illustrates explicit type casting in Scala.

Data Type Conversion in Scala

In Scala, explicit conversions are mandatory where there could be misunderstandings or potential data loss. To facilitate this task, Scala provides specific built-in methods.

Before we dive into conversions, let's briefly explain the Short and Byte data types as they haven't been covered previously. A Short is a 16-bit signed integer, which can hold values from -32,768 to 32,767. A Byte is an 8-bit signed integer, capable of storing values from -128 to 127. These types are generally used when you need to conserve memory in large arrays by sacrificing some range or precision.

Here are some of the conversion functions that Scala offers:

  • toByte: Converts value to Byte
  • toShort: Converts value to Short
  • toInt: Converts value to Int
  • toLong: Converts value to Long
  • toFloat: Converts value to Float
  • toDouble: Converts value to Double
  • toString: Converts value to String
  • toChar: Converts a code to a Character

Let's see an example demonstrating these functions:

@main def run: Unit =
  val number: Int = 65

  println(number.toFloat) // prints "65.0"
  println(number.toDouble) // prints "65.0"
  println(number.toLong) // prints "65"
  println(number.toChar) // prints "A"
  println(number.toString) // prints "65"

Each line in this code snippet explicitly converts the Int value 65 to another data type using Scala's built-in functions. The original data type of number remains an Int.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal