4 Values and variables

After reading this lesson, you’ll be able to

  • Write code that uses values to save and reuse computational results
  • Implement programs that carefully take advantage of variables

Scala has a clear separation between mutable and immutable assignments. In Scala, values are immutable: you cannot modify them after creating them. Variables are mutable: they can refer to different instances over time. Deciding when to declare a value rather than a variable is essential for your code to be fast and bug-free. Variables are more straightforward to use in your code because you can modify them. However, they can make your program extremely difficult to maintain and lead to errors when different processes try to do so simultaneously. ...

Get Get Programming with Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.