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.

Value
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,

Image1
Image 1

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

scala> val x: Int = 10
x: Int = 10

Variables
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.

scala> var x = 10
x: Int = 10

scala> println(x)
10
scala> x = 15
x: Int = 15

scala> x = x * 2
x: Int = 30

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> x = "Hello World"
<console>:11: error: type mismatch;
found    : String("Hello World")
required : Int
      x  : "Hello World"

Types

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’.

scala> var x = 5
x: Int = 5

scala> var y: Double = 10
y: Double = 10.0

scala> y = x
y: Double = 5.0

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,

scala> var l: Long = 100
l: Long = 100

scala> var x = 5
x: Int = 5

scala> x = l
<console>:12: error: type mismatch,
  found    : Long
  required : Int
         x = l
             ^

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.

scala> var a = "Hello Scala"
a: String = Hello Scala

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.

scala> a = a + "\n Welcome to Scala World"
a: String = Hello Scala
  Welcome to Scala World

scala> var compare = (a == "Is this string equal")
comapre: Boolean = false

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.

scala> var muliline = """This is string example
      | with multiple lines without using
      | backslash (\n) characters"""
multiline: String = 
This is string example
with multiple lines without using
backslash (\n) characters

 

 

 

Leave a Reply