SCALA – defining Value and Variables

In my previous post I give a brief introduction about SCALA, and show you how to write down your first program in SCALA using REPL (SCALA Command Promote). I hope you find it interesting to learn SCALA. c

Lets start with variables and core data types in SCALA. There are four terms which we need to understand before we start writing down the code. These terms are,

  1. Literal is data that directly appears in the source code. Like in my previous blog a text “Hello World”.
  2. Value is an immutable, that is a data can be assigned to a Value when it is defined but can never be reassigned.
  3. Variable is an mutable type, a value can be assigned when defining and reassign anytime later.
  4. Type is a definition of data on which we are working on.

Open the command promote and type SCALA. First we will see how we Value can be define in SCALA. Type the following line in shell

val val1: String = "Welcome to SCALA Value Type"

The output will be  val1: String = Welcome to SCALA Value Type . The syntax to define Value is val <name>: <type> = <literal>. val is the keyword and val1 is the name of Value of String type, and “Welcome to SCALA Value Type” is the literal assigned to the Value. As I mentioned above that Value can never be reassigned. If we try to change the value  val1 = "Try to change the value" we will get an error message as show in the Image 1 below,


Image 1

In the same way we can define the Value type of Int.

Variables are mutable, dynamic and re-assignable. Variables can be define by using keyword var. The syntax is as followed  var <identifier>[: <type>] = <data> . In below lines of code I define a variable “x” of type Int, assign a value and then reassign it.

Variables can be reassigned but the reassign value should be of compatible data type.  In the above example I declared the variable of type Int, lets assign a string value to it. SCALA will throw a type mismatch error as you can see below.


SCALA has both numeric and non numeric  data types to define Values and Variables. In SCALA there is no concept of primitive type.  Numeric data types include Byte, Short, Int, Long, Float and Double. Data type from lower to higher is automatically done. Below example I define variable’ x’ of type Int and a ‘y’ of type Double, and than assign the value of ‘x’ to ‘y’.

However automatic conversion of higher into lower datatype is permitted due to data lose. If you try to do automatic conversion SCALA will throw an error as show below,

However we can manually convert a higher datatype into lower, but this way there is a chance of losing data. To manually convert the data type we use toType method. To convert Long into Int  scala> x = l.toInt .

String is a most common datatype used in any programming language.  SCALA string datatype is built on Java’s String but include additional features like multi line literals. Strings will be included in double quotes.

To concatenate two String values plus operator (+) will be use. In same way to compare two String variables double equal is used (==).  While using double quote special characters with backslash is used.

In Scala like Python language triple quotes are used to display string as it is. It means if you typing a string in multiple lines same as it is it will be displayed no need to use backslashes. In triple quotes special characters with backslashes are not recognized.




Posted in SCALA Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *