Scala Lesson 5 – Data Types | Dataplexa

Data Types in Scala

In this lesson, you will learn about data types in Scala. Data types define what kind of values a variable can store and what operations can be performed on it.

Scala is a statically typed language, meaning every variable has a type that is checked at compile time. This helps catch errors early and makes programs more reliable.


What Is a Data Type?

A data type tells the compiler:

  • What kind of value is stored
  • How much memory is required
  • What operations are allowed

For example, numbers, text, and true/false values all use different data types.


Scala Type System Overview

Scala has a rich and unified type system. All data types ultimately inherit from a common root type called Any.

At a high level, Scala data types are divided into:

  • Value Types (AnyVal)
  • Reference Types (AnyRef)

Numeric Data Types

Numeric types are used to store numbers. Scala supports both integer and floating-point numbers.


Int

Int is the most commonly used integer type. It stores whole numbers.

val age: Int = 25
println(age)

Here, age stores a whole number and cannot store decimal values.


Long

Long is used for very large whole numbers.

val population: Long = 7800000000L
println(population)

Notice the L suffix, which tells Scala the number is a Long.


Double

Double is used for decimal numbers.

val price: Double = 99.99
println(price)

This type is commonly used for calculations that require precision.


Float

Float stores decimal numbers but uses less memory than Double.

val temperature: Float = 36.5f
println(temperature)

The f suffix indicates a Float value.


Boolean Data Type

The Boolean type stores logical values: true or false.

val isLoggedIn: Boolean = true
println(isLoggedIn)

Booleans are commonly used in conditions and decision-making logic.


Character Data Type

Char stores a single character. It must be enclosed in single quotes.

val grade: Char = 'A'
println(grade)

Characters are useful for representing single letters or symbols.


String Data Type

String stores text values. Strings are immutable in Scala.

val name: String = "Dataplexa"
println(name)

Once created, a string value cannot be changed.


Type Inference with Data Types

Scala can automatically infer the data type based on the assigned value.

val count = 10
val title = "Scala Course"
val active = false

Even though types are not written explicitly, Scala still knows them internally.


Checking the Type of a Variable

You can check the type of a value at runtime using getClass.

val x = 100
println(x.getClass)

This is useful for debugging and understanding inferred types.


Type Conversion

Scala does not perform automatic unsafe type conversions. You must convert types explicitly.

val num = 10
val result = num.toDouble
println(result)

Here, an integer is converted into a Double using toDouble.


Why Data Types Matter

Correct use of data types:

  • Prevents runtime errors
  • Improves performance
  • Makes code easier to read
  • Helps tools and compilers optimize code

📝 Practice Exercises


Exercise 1

Create an Int variable and print its value.

Exercise 2

Create a Double variable and convert it to Int.

Exercise 3

Create a Boolean variable and print it.


✅ Practice Answers


Answer 1

val number: Int = 50
println(number)

Answer 2

val value: Double = 45.6
val intValue = value.toInt
println(intValue)

Answer 3

val status: Boolean = true
println(status)

What’s Next?

In the next lesson, you will learn about Strings in Scala, including string operations and interpolation.