Scala has two kinds of variables, vals and vars. A val is similar to a final variable in Java. Once initialized, a val can never be reassigned. A var, its more likely var variables in C#. A var can be reassigned throughout its lifetime. Here’s a val definition: Figure 1
Loops with while and for decision if
Iterate with foreach and for
Basically all operations are methods call in scala
- Mutated (:::) Basically it’s like ‘Python Extends’. It will include two list and will return with a new list.
- Cons (::) Cons prepend a new element to the beginning of an existing list, and return the resulting list.
To get more details on list check the scala api doc
If you are a Python programmer you would have probably know about this container. It’s a very useful container object. Like lists, tuples are immutable, but unlike lists, tuples can contain different types of elements.
Mutable and Immutable
Mutable and immutable are English words meaning “can change” and “cannot change” respectively. The meaning of the words is the same in the IT context; i.e.
- a mutable string can be changed, and
- an immutable string cannot be changed.
- For example, arrays are always mutable; lists are always immutable
Sets and Maps
Now that we have seen some scala code, you can try and have fun with scala ☺. In my next blog lets dive into more details on class and objects.