Course Content
Introduction to Scala
Introduction to Scala
Basic Data Types
Variables
Before we dive into exploring various data types, let’s first discuss what variables are.
You can think of a variable in programming like a box where you can store some value.
Let’s now take a look at the process of creating a variable:
- First, a variable should be named. For example, if you want to store someone's age, you might name your variable
age
. - Next, similarly to boxes, you need to choose the right size and type for your variable, more formally, its data type.
- Finally, once you have your box (variable) and know what type it is, you can put something inside. With variables, you assign a certain value to your variable, e.g. assign
18
to theage
variable.
Here is how the general syntax for creating a variable in Scala looks like:
Main
val variable_name: variable_type = value
Variable Naming Conventions
Similarly to other programming languages, Scala has certain conventions (rules) regarding the names of the variables:
- Variable names in Scala should start with a lowercase letter and contain only letters and numbers.
- Scala predominantly uses camel case naming. In camel case, multi-word names have their first word in lowercase and the first letter of each subsequent word capitalized.
- Scala keywords cannot be used as variable names. These include words like
if
,else
,match
,try
,class
,def
,val
,var
etc. - Use descriptive and meaningful names for variables. For example,
bookPrice
is a better name thanbp
.
Data Types
Since we have already mentioned data types, let's discuss what they actually are.
Before we take a look at the basic data types in Scala, let's first make it clear what a byte is.
Here are the basic data types in Scala:
Byte
- An integer data type in Scala, it has a range from -128 to 127 (256 values) and uses 1 byte of memory.
Main
val myVariable: Byte = 10
Short
- Also an integer data type, it can accommodate values between -32,768 and 32,767, requiring 2 bytes of memory.
Main
val myVariable: Short = 1000
Int
- The most commonly utilized integer data type in Scala, capable of holding values from -2^31 to -2^31 - 1 and uses 4 bytes of memory.
Main
val myVariable: Int = 12546539
Long
- The largest integer data type in Scala, it can store values from -2^63 to 2^63 - 1 and occupies 8 bytes of memory.
Main
val myVariable: Long = -234351987
Float
- A floating-point data type in Scala that can store up to 7 decimal digits and occupies 4 bytes of memory. When declaring aFloat
, it's important to append an F or F at the end of the number, likeval myFloat: Float = 2.56f
.
Main
val myVariable: Float = 10.5f
Double
- This data type can store up to 15 decimal digits and is a double-precision floating-point number in Scala. It uses 8 bytes of memory. In Scala, there's no need for a letter suffix when declaring aDouble
.
Main
val myVariable: Double = 20.5
Boolean
- In Scala, theBoolean
data type can only represent two values:true
orfalse
. The actual size in memory can vary from 1 bit to 1 byte.
Main
val myVariable: Boolean = true
Char
- A character data type in Scala, it stores characters using the Unicode standard and occupies 2 bytes of memory. We'll discuss this data type in detail in the following chapters.
Main
val myVariable: Char = 'A'
Here is a table which summarizes the information regarding Scala data types:
Let's now take a look at an example of creating three different variables and printing them on separate lines:
Main
object Main { def main(args: Array[String]): Unit = { val age: Int = 16 val height: Float = 1.8F val letter: Char = 'C' println(age) println(height) println(letter) } }
Thanks for your feedback!