Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Data Types | Variables and Data Types
Introduction to Scala
course content

Contenido del Curso

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

bookBasic 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:

  1. First, a variable should be named. For example, if you want to store someone's age, you might name your variable age.
  2. Next, similarly to boxes, you need to choose the right size and type for your variable, more formally, its data type.
  3. 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 the age variable.

Here is how the general syntax for creating a variable in Scala looks like:

java

Main

copy
1
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 than bp.

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.
java

Main

copy
1
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.
java

Main

copy
1
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.
java

Main

copy
1
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.
java

Main

copy
1
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 a Float, it's important to append an F or F at the end of the number, like val myFloat: Float = 2.56f.
java

Main

copy
1
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 a Double.
java

Main

copy
1
val myVariable: Double = 20.5
  • Boolean - In Scala, the Boolean data type can only represent two values: true or false. The actual size in memory can vary from 1 bit to 1 byte.
java

Main

copy
1
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.
java

Main

copy
1
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:

java

Main

copy
12345678910
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) } }

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 1
some-alt