Scala Expressions, Variables and Basic Types

We’ll use Scala REPL for entering Scala expressions and seeing what the result of evaluating them is. REPL stands for Read-Eval(uate)-Print-Loop, which means it is a program that
(1) reads the expressions you type in,
(2) evaluates them using the Scala Compiler,
(3) prints out the result of an evaluation, and then
(4) waits for you to enter further expressions.

=====================================================
Scala in Action from Command Prompt
=====================================================

 
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>scala
	Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
	Type in expressions for evaluation. Or try :help.
scala> 

Comments: The scala> line is the prompt that the REPL is waiting for you to enter expressions.

=====================================================
Case Study – 1
=====================================================

	
scala> "Hello World"
	res8: String = Hello World

scala> 2017
	res9: Int = 2017

scala> "2000"
	res10: String = 2000

scala> 3+4
	res11: Int = 7

scala> "2" + "3"
	res13: String = 23

scala> println("Hi,\nGood Morning");
	Hi,
	Good Morning	

=====================================================
Case Study – 2
=====================================================

	
scala> print ("Hello World")
	Hello World
scala> print (3+4)
	7

Comments: Following is the workflow for above statement:- 
	(1) Scala evaluates 3 + 4, which is 7.
	(2) Scala passes that value to the command prompt.
	(3) print outputs “7"

We often need to store the result of evaluating an expression to a variable for later use
scala> val x = 3+4
	x: Int = 7
scala> print(x)
	7

Here, x is a variable, which we’ve indicate by prefacing it with val, which indicates it is a fixed variable whose value cannot change. We  can choose the names for variables, but they must follow some rules.
    (1) Variable names may contain: letters, numbers, underscore
    (2) They should not start with a number
    (3) They should not be identical to one of the “reserved words” that Scala has already defined, such as for, if, val, var, etc.

***********************************
Few Working Tips
***********************************
scala> print(x)
	5
scala> print (x+5)
	10
scala> x+10
	res23: Int = 15
scala> x
	res25: Int = 5
scala> 7/2
	res26: Int = 3
scala> 7.0/2.0
	res27: Double = 3.5

=====================================================
Case Study – 3
=====================================================

 	
scala> val a : Int = 20    // We are doing naming here with help of "val a"
	a: Int = 20

Syntax :- val <identifier> [:<type>] = <expression>

scala> print (a)
	20
scala> a /2
	res29: Int = 10

Comments: The a : Int portion of the line indicates that the variable a has the type Int. Here are some examples of other variables with different types.

scala> val b: Double = 2.25
	b: Double = 2.25

scala> val c: String = "Hello world"
	c: String = Hello world

scala> val pi: Int = 3.14
	<console>:11: error: type mismatch;
	 found   : Double(3.14)
	 required: Int
		   val pi : Int = 3.14
						  
Comments: Importantly, we cannot assign the variable a type that conflicts with the result of the expression. Here, we try to assign a Double value to a variable of type Int, and Scala reports an error.
					  
scala> val pi: Double = 3.14
	pi: Double = 3.14

scala> val atSymbol: Char = '@'
	atSymbol: Char = @

scala> val l: Long = 20
	l: Long = 20

scala> val i: Int = l.toInt
	i: Int = 20		

=====================================================
Case Study – 4
=====================================================

 
In addition to variables declared with val, Scala allows variables to be declared with var — these variable can have their values reassigned. A few examples are the easiest way to see the difference and syntax is var <identifier>[:<type>] = <expression>

scala> val a = 5
	a: Int = 5

scala> a = 10
<console>:12: error: reassignment to val
       a =10
         ^
scala> var b = 5
	b: Int = 5
	
scala> b = 10
	b: Int = 10

scala> b
	res31: Int = 10

=====================================================
Case Study – 5
=====================================================

 
scala> val x: Int = 5
	x: Int = 5

scala> x 
	res0: Int = 5

scala> x * 2
	res1: Int = 10

scala> res0 * res1
	res2: Int = 50

=====================================================
Case Study – 6
A multiline String can be created using triple-quotes. Multiline strings are literal, and so do not recognize the use of backslashes as the start of special characters:

=====================================================

 
scala> val greeting = """She suggested reformatting the file
     | by replacing tabs (\t) with newlines (\n);
     | "Why do that?", he asked. """
greeting: String =
"She suggested reformatting the file
by replacing tabs (\t) with newlines (\n);
"Why do that?", he asked. "	

=====================================================
Case Study – 7 :: String Interpolation
=====================================================

 
scala> val item = "apple"
	item: String = apple

scala> s"How do you like ${item}s?"
	res0: String = How do you like apples?

scala> s"Fish n chips n vinegar, ${"pepper "*3}salt"
	res1: String = Fish n chips n vinegar, pepper pepper pepper salt

=========================================================================
Case Study – 8 :: An Overview Of Scala Types
=========================================================================

 
scala> val isTrue = !true
isTrue: Boolean = false

scala> val isFalse = !true
isFalse: Boolean = false

scala> val unequal = (5 != 6)
unequal: Boolean = true

scala> val isLess = (5 < 6) 
isLess: Boolean = true 

scala> val unequalAndLess = unequal & isLess
unequalAndLess: Boolean = true

scala> val definitelyFalse = false && unequal
definitelyFalse: Boolean = false
Advertisements