Thursday, 20 October 2016

Basics of Scala : Defining a Variables in Scala : Day 2 Learnings

In computer science the term variable typically refers to a unique identifier corresponding to an allocated or reserved memory space, into which values can be stored and from which values can be retrieved. As long as the memory space is reserved, it can be assigned new values over and over again. Thus, the contents of the memory space are dynamic, or variable.

In most languages, such as C, Java, PHP, Python, and Ruby, this is the typical pattern for working with named, assignable memory storage. Variables are dynamic, mutable, and reassignable (with the exception of those defined with special restrictions such as Java’s final keyword).

In Scala, values are preferred over variables by convention, due to the stability and predictability they bring to source code. When you define a value you can be assured that it will retain the same value regardless of any other code that may access it. Reading and debugging code is easier when a value assigned at the beginning of a code segment is unchanged through the end of the code segment.

Finally, when working with data that may be available for the life span of an application, or accessible from concurrent or multithreaded code, an immutable value will be more stable and less prone to errors than mutable data that may be modified at unexpected times.

However, in those places where variables are more suitable, such as local variables that store temporary data or accumulate values in loops, variables will certainly be used. Now that the preference for values over variables has been explained in detail, we can put that aside and cover how to use variables in Scala.

The var keyword is used to define a variable with a given name, type, and assignment.

Syntax: Defining a Variable

  1. var <identifier>[: <type>] = <data>


Like values, variables can be defined with or without an explicit type. If no type is specified the Scala compiler will use type inference to determine the correct type to assign to your variable. Unlike values, variables can be reassigned new data at any time.

Here is an example of defining a variable and then reassigning it, in this case to the product of itself and another number:

  1. scala> var x = 5
  2. x: Int = 5
  3. scala> x = x * 4
  4. x: Int = 20


Although a variable can be reassigned, its designated type cannot, and so a variable cannot be reassigned data that has an incompatible type. For example, defining a variable of type Int and then assigning it a String value will result in a compiler error:


  1. scala> var x = 5
  2. x: Int = 5
  3. scala> x = "what's up?"
  4. <console>:8: error: type mismatch;
  5. found: String("what\'s up?")
  6. required: Int x = "what's up?"
  7. ^


However, defining a variable of type Double and assigning it an Int value will work because Int numbers can be converted to Double numbers automatically:

  1. scala> var y = 1.5
  2. y: Double = 1.5
  3. scala> y = 42
  4. y: Double = 42.0


Share this article with your friends.
Related Posts Plugin for WordPress, Blogger...