Monday, 19 September 2016

SCALA BASICS DAY 1 Practice on 19 Sept 2016



















--------------------------------------------------------

scala> var id = 1
id: Int = 1

scala> var id = 1l
id: Long = 1

scala> var id = 1f
id: Float = 1.0

scala> var id = 1d
id: Double = 1.0

--------------------------------------------------------

scala> var id = 1
id: Int = 1

scala> var id : Long = 1
id: Long = 1

scala> var id : Double = 1
id: Double = 1.0

scala> var id : Float = 1
id: Float = 1.0


scala> var id = 1
id: Int = 1

scala> id.toLong
res0: Long = 1

scala> id.toDouble
res1: Double = 1.0

scala> id.toFloat
res2: Float = 1.0

scala> id.toString
res3: String = 1

--------------------------------------------------------

scala> var id = 1
id: Int = 1

scala> var name = "kalyan"
name: String = kalyan

scala> var age = 30
age: Int = 30

scala> var salary = 10000
salary: Int = 10000

scala> var loc = "hyd"
loc: String = hyd


scala> var details = "name is " + name + " , location is " + loc
details: String = name is kalyan , location is hyd

scala> var details = s"name is $name , location is $loc"
details: String = name is kalyan , location is hyd



scala> var info = name + loc
info: String = kalyanhyd

scala> var info = (name).+(loc)
info: String = kalyanhyd

--------------------------------------------------------

scala> (x : Int) => { x + 1 }
res4: Int => Int = <function1>

scala> val f = (x : Int) => { x + 1 }
f: Int => Int = <function1>

scala> f(1)
res5: Int = 2

scala> def f ( x : Int ) = { x + 1 }
f: (x: Int)Int

scala> f(1)
res6: Int = 2


--------------------------------------------------------

scala> def add (x : Int, y : Int ) = { x + y }
add: (x: Int, y: Int)Int

scala> add( 1, 2)
res7: Int = 3

scala> def add (x : Int, y : Int ) : Int = { x + y }
add: (x: Int, y: Int)Int

scala> add( 1, 2)
res8: Int = 3

scala> def add (x : Int, y : Int ) : Double = { x + y }
add: (x: Int, y: Int)Double

scala> add( 1, 2)
res9: Double = 3.0

--------------------------------------------------------

scala> if ( id > 1 ) { print("more than 1") } else { print("less than 1") } 
less than 1
scala> id = 2
id: Int = 2

scala> if ( id > 1 ) { print("more than 1") } else { print("less than 1") } 
more than 1
scala> 


scala> def fact(n : Int) : Int = {
     | if( n == 1 ) 1
     | else n * fact(n - 1)
     | }
fact: (n: Int)Int

scala> fact(5)
res12: Int = 120

scala> fact(4)
res13: Int = 24



scala> def fact(n : Int) : Int = { if ( n == 1) 1 else n * fact(n-1) }
fact: (n: Int)Int

scala> fact(5)
res16: Int = 120

scala> fact(4)
res17: Int = 24


--------------------------------------------------------

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