1. Immutable variables (val) and value types
Let's define variables and explore types now.
2. The game of Twenty-One
First, we need more details on the game of Twenty-One for our program. The rules you need to know: there is a deck of cards. Each card has a point value. 2s are worth 2, 3s 3, etc. through 10. Face cards are worth 10. Aces are worth 1 or 11, player's choice. Each player is dealt two cards to start, with the option to "hit" for another card or to "stay" otherwise. The aim and name of the game: get 21 points or as close to it as possible. If you exceed 21, you "bust" and can't win.
3. Scala has two kinds of variables
Scala has two kinds of variables: vals and vars. vals are immutable, which means once initialized, vals can't be reassigned. In Twenty-One, a val is like any non-ace card. The value of the 4 of hearts will always be 4 points. One way to define the val fourHearts is by typing val fourHearts colon Int, then equals 4. If you're familiar with Java, a val is kind of like a final variable.
4. Reassigning a val produces an error
Since fourHearts is a val, the Scala interpreter yells at us when we try to reassign it. Error: reassignment to val.
5. Scala value types
These are all nine of Scala's value types. You saw fourHearts was an Int.
6. Scala value types
All variables have types. For data-related tasks, the four most common types are Double, Int, Boolean, and String.
7. Double
Double is the default floating point value type that Scala offers. Ignore these technical definitions if they don't mean anything to you now. We won't need Double for our program,
8. Double
so here's an example using pi. 3.14 is a Double.
9. Double
Scala nudges us to use Double, and throws a type mismatch error if we give type Float to the value 3.14. One reason for the nudge:
10. Double is more precise than Float
Double is more precise than Float. Double stores pi to 15 points beyond the decimal. Float, specified by adding an f at the end, stores pi to 7 points.
11. Int
Int is an integer. These range from negative 2 billion and change to positive two billion and change.
12. Int
Again, the integer 4 (for the point value of the four of hearts) is an Int.
13. Boolean
Boolean is true or false. In Twenty-One, a Boolean can represent whether or not a player's hand busts.
14. Char and String
It is rare that you define a Char, but you'll define a String often, which is a sequence of Chars in Scala. The letter A followed by the Unicode character for spades in quotes is an example of a String.
15. Scala value types
Let's focus on Int to understand how types are structured in the Scala standard library.
16. Scala value types
The type Int names the class Int in the package scala. The full name of Int is scala dot Int, but you can use the simple name Int since the package scala is automatically imported into every Scala source file.
17. Scala value types
Previously, you learned that every value in Scala is an object. Here, fourHearts is an object. That is, an instance of class Int. So is fiveHearts.
18. Scala value types have equivalent Java types
All of these Scala types (except Unit, which you'll learn about later) have equivalent Java primitive types that live in the package java dot lang. When you compile Scala code to Java bytecode, the Scala compiler will use these Java types where possible which is HUGE for code performance.
19. Choosing the right type
In later courses, you'll experience how choosing the right type makes programs smaller and faster.
20. Let's practice!
You've got the hang of vals and value types now. Go practice!