Tuesday, 18 October 2016

Basics of Scala : Definitions of the terms literal, value, variable, and type in Scala : Day 2 Learnings

Let’s start with the definitions of the terms literal, value, variable, and type:

A literal (or literal data) is data that appears directly in the source code, like the number 5, the character A, and the text “Hello, World.”

A value is an immutable, typed storage unit. A value can be assigned data when it is defined, but can never be reassigned.

A variable is a mutable, typed storage unit. A variable can be assigned data when it is defined and can also be reassigned data at any time.

A type is the kind of data you are working with, a definition or classification of data. 

All data in Scala corresponds to a specific type, and all Scala types are defined as classes with methods that operate on the data.

The data stored in values and variables in Scala will get automatically deallocated by the Java Virtual Machine’s garbage collection when they are no longer used. There is no ability, or need, to deallocate them manually.

Let’s try exercising these terms by working with data in the Scala REPL. 

Scala values are defined with the syntax 

  1. val <name>: <type> = <literal> 

so we will create a value with the name x , type Int (short for “integer”), and assigned it the literal number 5:

  1. scala> val x: Int = 5
  2. x: Int = 5

What happened here? The REPL (again, a Read-Evaluate-Print-Loop shell) read the value definition, evaluated it, and reprinted it as a confirmation. 

The new value, named x , is now defined and available to use. So let’s use it:

  1. scala> x
  2. res0: Int = 5 scala> x * 2 res1: Int = 10 scala> x / 5 res2: Int = 1

Each of these three input lines are valid Scala syntax and return an integer value. In each case, because a value is returned, the REPL repeats the value and its type and also assigns a unique, sequentially named value starting with res0 (short for “result”). 

You can choose to make use of these “result” values just like any value you explicitly define:

  1. scala> res0 * res1
  2. res3: Int = 50

Here the values res0 and res1 are multiplied, resulting in the value 50 being returned and stored in the new value named res3 .

Let’s try working with variables now. Variables, which unlike values are mutable and can be reassigned new values, are defined with the syntax 

  1. var <name>: <type> = <literal> 

Here is an example of working with variables:

  1. scala> var a: Double = 2.72
  2. a: Double = 2.72 scala> a = 355.0 / 113.0 a: Double = 3.1415929203539825 scala> a = 5 a: Double = 5.0

In this example we defined the variable a to have the type Double , a double-precision floating-point number. And then, because it is a variable, we reassigned it to a different value.

This has been a short introduction to using values, variables, types, and literals in Scala.

I will cover each of these subject areas in depth next articles.

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