First Interaction with scala
- repl
- variable and values
- conditionals
- loops
scala> 5+10;
res3: Int = 15
scala> 5+10
res4: Int = 15
scala> "Hello"+" world"
res5: String = Hello world
scala> res3*10
res6: Int = 150
scala> println("hi")
hi
Scala variables
- var- Assignment can change
- val- Assignment can’t change. (more like final in java)
scala> var str="something"
str: String = something
scala> str="reassigned"
str: String = reassigned
scala> val y=1
y: Int = 1
scala> y=2
<console>:12: error: reassignment to val
y=2
^
Conditionals
- if is an expression (not statement) i.e it return/value as value
- In scala we donot need explicit return statement to return a value
- last statement becomes the return statement
- If there is 1 statement we donot need to provide {} braces nor the semicolon ;
scala> :paste
// Entering paste mode (ctrl-D to finish)
val args=Array("Monday")
val day=if(!args.isEmpty){
args(0);
}else{
"sunday"
}
// Exiting paste mode, now interpreting.
args: Array[String] = Array(Monday)
day: String = Monday
loops
Pages: Page 1, Page 2, Page 3, Page 4