Scala

First Interaction with scala

  1. repl
  2. variable and values
  3. conditionals
  4. 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

  1. var- Assignment can change
  2. 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

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment