Scala
Scala
Scala is typed
A programming language is said to use static typing when type checking is performed
during compile-time as opposed to run-time
Runs on JVM, full inter-op with java
object Oriented
Functional
Use existing Java libraries
Use existing java tools (Ant,Maven,JUnit,etc..)
Decent IDE Support(NetBeans,IntelliJ,Eclipse)
res0: Int = 5
scala> res0*2
res1: Int =10
scala> println(msg)
Hellow World
Byte 8-bit signed 2�s complement integer. It has minimum value of �128 and a
maximum value of 127 (inclusive).
Short 16-bit signed 2�s complement integer. It has a minimum value of �32,768 and
maximum of 32,767 (inclusive).
Int 32-bit signed 2�s complement integer. It has a minimum value of �2,147,483,648
and a maximum value of 2,147,483,647
Long 64-bit signed 2�s complement integer. It has a minimum value of
-9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807
(inclusive).
Float A single-precision 32-bit IEEE 754 floating point.
Double A double-precision 64-bit IEEE 754 floating point.
Boolean Two possible values: true and false.
Char A single 16-bit Unicode character. I
If you realize you have typed something wrong, but the interpreter is still
waiting for more input, you can escape by pressing enter twice:
scala> :quit
Scala Scripts
firstscript.scala
args.foreach(println)
Array
scala> array
scala> dim.foreach(_.foreach(println))
All lists can be defined using two fundamental building blocks, a tail Nil and ::,
which is pronounced cons.
list methods
tail:This method returns a list consisting of all elements except the first.
scala> fruit.head
scala>fruit.tail
scala>fruit.isEmpty
scala> val fruit = List.fill(3)("apples") // Repeats apples three times.
set
a Set is a collection that contains no duplicate elements. There are two kinds of
Sets, the immutable and the mutable.
Set is not an ordered collection - you can't get element by index.
methods
head
tail
isEmpty
concatination set
set.intersect method
ifelse
object IfElse {
def main(args:Array[String]){
val x:Int =20
var res =""
if (x ==20)
{
res="x==20"
} else{
res="x !=20"
}
println(res)
}
}
object IfElse {
def main(args:Array[String]){
val x =20
var res= ""
if (x ==20)
{
res="x==20"
} else{
res="x!=20"
}
println(res)
}
}
object IfElse {
def main(args:Array[String]){
val x =20
val y=30
var res= ""
if (x >y) {
res="x > 5"
}
else if(x < y) {
res="x <y"
} else {
res ="x ==y"
}
println(res)
}
}
object IfElse {
def main(args:Array[String]){
val x =20
val y=30
var res= ""
if (x >y) res="x > 5"
else if(x < y) res="x <y"
else res ="x ==y"
println(res)
}
}
object IfElse {
def main(args:Array[String]){
val x =30
val y=30
var res= ""
if (x ==20 && y==30 ) res="x ==20 && y==30"
Loops
while loop
object IfElse {
def main(args:Array[String]){
var x =0
while (x <10){
println("x= "+x)
x+=1
}
}
}
object IfElse {
def main(args:Array[String]){
var x =10
while (x >0){
println("x= "+x)
x-=1
}
}
}
ForLoop
object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 10){
println("x="+x)
}
object ForLoop {
def main(args:Array[String]){
for(x <- 1 until 10){
println("x="+x)
}
object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ; y <- 1 to 3 ){
println("value of x "+x)
println("value of y "+y)
object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ){
object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ){
object ForLoop {
def main(args:Array[String]){
println(x)
}
}
object ForLoop {
def main(args:Array[String]){
println(x)
}
}
import scala.util.control._
object ForLoop {
def main(args:Array[String]){
loop.breakable{
for (x <- 1 to 10){
println(x)
if (x==5){
loop.break
}
}
continue example
object ForLoop {
def main(args:Array[String]){
case statement
object demo {
def main(args:Array[String]){
val i =4
var month=""
i match {
case 1 => month="January"
case 2 => month="February"
case 3 => month="March"
case 4 => month="April"
case 5 => month="May"
case 6 => month="June"
case 7 => month="July"
case 8 => month="August"
case 9 => month="September"
case 10 => month="October"
case 11 => month="November"
case 12 => month="December"
case _ => month="Invalid month" // the default, catch-all
}
println(month)
}
object demo {
def main(args:Array[String]){
val i =2
println(month)
}
}
import scala.util.Random
object demo{
def main(args:Array[String]){
val x: Int = Random.nextInt(10)