Welcome to our lesson on Scala's basic data types. In this lesson, we'll delve into numbers, booleans, characters, strings, and arrays. Additionally, we'll explore how we can declare these data types — either explicitly or implicitly. These fundamentals will enable you to create and manipulate data effectively in your Scala programs.
In Scala, numbers are categorized by their memory sizes and value ranges. Integral numbers such as Int
and Long
require 4 bytes and 8 bytes of memory, respectively. For numbers with decimal points, we have Float
and Double
. The Float
type requires 4 bytes of memory and supports approximately seven decimal places, while Double
requires 8 bytes of memory and can handle around 15 decimal places.
Here's an example illustrating the different number types:
Scala1@main def run: Unit = 2 val intNumber: Int = 300 // This stores numbers from -2147483648 (-2^31) to 2147483647 (2^31 - 1) 3 val longNumber: Long = 100000000L // This stores numbers from -9223372036854775808 (-2^63) to 9223372036854775807 (2^63 - 1) 4 val floatNumber: Float = 2.5F // This requires 4 bytes of memory 5 val doubleNumber: Double = 5.5 // This requires 8 bytes of memory
Starting with Scala 2.13, you can use underscores to make large numbers more readable:
Scala1val formattedNumber: Int = 100_000
The Boolean data type represents truth values as true
or false
. The Char data type, represented by Char
, holds a single character, while String
stores sequences of characters. It's important to note that Chars are defined using single quotes ('
), and Strings are defined using double quotes ("
).
Scala1@main def run: Unit = 2 val isScalaFun: Boolean = true 3 val letter: Char = 'S' 4 val hello: String = "Hello, Scala!"
In Scala, you can explicitly declare data types by specifying the type after the variable name. Although type inference is available, it is not applicable for long numbers, so you always have to specify Long
explicitly:
For example:
Scala1@main def run: Unit = 2 val explicitInt: Int = 239 // The data type is explicitly declared as Int 3 val implicitInt = 239 // The data type is implicitly declared as Int, thanks to type inference. 4 5 val explicitLong: Long = 3000000000L // The data type needs to be explicitly declared as Long. Scala requires explicit declaration for Long variables, even if the number is within the Int range. 6 val anotherLong: Long = 239L // Though 239 fits into the Int type, in Scala we still need to explicitly declare it as Long by appending `L` at the end. 7 8 val explicitString: String = "Explicit" // The data type is explicitly declared as String 9 val implicitString = "Implicit" // Scala infers the type as String
We have covered the basics of data types in Scala, delving into numbers, booleans, characters, and strings. We have also explored explicit and implicit type declarations. As the next step, look forward to exciting practice exercises that will solidify your understanding and offer practical exposure. Let's delve deeper into Scala!