Variable
This Scala code creates a value named "x" and assigns it the value of 5.
The "val" keyword is used to declare an immutable variable in Scala, which means that once the value is assigned, it cannot be changed.
Here's an example of how you can use this value in Scala:
val x = 5
println(x) // Output: 5
In this example, the value of "x" is printed to the console using the "println" method. Since "x" is immutable, you cannot reassign it to a new value later in the code.
Inference
Scala is a statically-typed programming language, which means that every variable and expression must have a declared type. However, Scala also supports type inference, which allows the compiler to deduce the type of an expression automatically based on its context.
Type inference in Scala is performed by the compiler using a process called type unification. When a variable or expression is declared without an explicit type, the compiler attempts to infer the type by examining the types of other expressions in the same context. For example, consider the following code snippet:
val x = 5
val y = "Hello, world!"
val z = x + y
In this case, x is assigned an integer value, so its type is inferred to be Int. y is assigned a string value, so its type is inferred to be String. When the expression x + y is assigned to z, the compiler determines that the + operator is only defined for two arguments of numeric types, so it infers that x must also be a String and produces a compile-time error.
Type inference in Scala can greatly reduce the amount of boilerplate code required for variable and function declarations while still maintaining strong type safety. However, it's important to keep in mind that type inference is not always possible, particularly in complex or ambiguous situations, so it's still necessary to understand the type system and use explicit type annotations when appropriate.
Immutable
val
A val is an immutable variable. A constant.
It can not be reassigned and will give an error when you try.
scala> x = 4
-- `[E052]` Type Error: ----------------------------------------------------------
1 |x = 4
|^^^^^
|Reassignment to val x
Variable Names
The Scala variable names follow the Java conventions:
- The name of a variable must begin with a letter, a dollar sign $ or a underscore _
- The following characters can be letters or numbers. For example: number, A1, $value, _number
- A letter is a Unicode character. This means that you can choose from 34,186 characters
- White space is not allowed in variable name.
- Uppercase, lowercase and accented letters are allowed.
- The maximum length is unlimited
- Scala distinguishes uppercase and lowercase letters, so KM, Km, kM, and km are different names.
- Keywords are not allowed.
Valid names
number, thisIsANumber, this_is_a_number, n1, A1, $value, _number
Invalid names
1value, a.b, a!, b?, type
Number Types
Scala is a strong type language. Every variable must has a type.
We first take a look at the basic number types inherited from Java.
| naam | role | size(bits) | min to max value |
|---|---|---|---|
| Byte | whole | 8 | -128 to +127 |
| Short | whole | 16 | 32.768 to 32.767 |
| Int | whole | 32 | -2.147.483.648 to +2.147.483.747 |
| Long | whole | 64 | -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808 |
| Float | real | 32 | 3,40292347E+38 to +3,40292347E+38 |
| Double | real | 64 | -1,79769313486231570E+308 to +1,79769313486231570E+308 |
| Char | letter | 16 | Unicode '\u0000 to '/uFFFF |
| Boolean | truth value | true or false |
When you need a whole number take an Int and only when an Int is too small take a Long.
My advice is not to use a Byte and Short, because there are some corner cases.
When you work with texts you will use a String.
Most libraries will have functions on String and not on Char even if the text contains only one letter.
Variable Exercises
Examples
Create a val called pi and assign it the value of 3.14159:
val pi: Double = 3.14159
Create a val called message and assign it a string value:
val message: String = "Hello, world!"
Create a val called age and assign it an integer value:
val age: Int = 30
Create a val called isTall and assign it a boolean value:
val isTall: Boolean = true
Create a val called name and assign it a string value, then print a message that includes the value of name:
val name: String = "Alice"
println(s"My name is $name.")
In this example, the string interpolation syntax is used to include the value of name in the printed message. The dollar sign ($) followed by the variable name is replaced with the value of the variable. The s at the beginning of the string indicates that this is a string interpolation.