Interpreter
scala>8 * 5 + 2 res0: Int=42 scala> 0.5 * res0 res1: Double = 21.0 scala> "Hello, " + res0 res2: String = Hello, 42 scala> res2.toUpperCase res3.toUpperCase
Values and Variables
scala> val answer=8*5+2 answer: Int = 42 scala> 0.5*answer res0: Double = 21.0 scala> res0=0 <console>:13: error: reassignment to val//res0 is val, not var scala> answer=0 <console>:12: error: reassignment to val scala> var counter=0 counter: Int = 0 scala> counter=1 counter: Int = 1
You are encouraged to use a val unless you really need to change the contents. The type of a variable or function is always written after the
name of the variable or function. For example
scala> val greeting: String = null greeting: String = null scala> val greeting: Any = "Hello" greeting: Any = Hello
We can declare mutiple vars or vals which can also need not specify the type, like
scala> val xmax, ymax = 100 // need not specify the type xmax: Int = 100 ymax: Int = 100 scala> var greeting, message: String = null//greeting and message are both strings, initialized with null greeting: String = null message: String = null
Common Used Types
7 numeric types: Byte , Char , Short , Int , Long , Float , and Double , and a Boolean type. However, unlike Java, these types are classes. There is no distinction between primitive types and class types in Scala.
scala> 1.toString() // Yields the string "1" res6: String = 1 scala> 1.to(10) // Yields Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) res8: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Scala String vs Java String: String in Scala is inherited from Java String, but augmented with StringOps class with well over a hundred operations. So Scala String is much more powerful than Java String. Like
scala> "Hello".intersect("World") // Yields "lo" res10: String = lo
Similarly, there are classes RichInt , RichDouble , RichChar , and so on to rich Int, Double, Char and so on. For example of RichInt:
scala> 1.to(10) res11: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Converting between numeric types: Using methods not casts. Like
scala> 99.44.toInt res12: Int = 99 scala> 99.toChar res13: Char = c scala> "99.44".toDouble res14: Double = 99.44
Arithmetic and Operator Overloading
The operators in Scala are methods:+ - * / % & | ^ >> << a method b <--> a.method(b)
For example:
scala> 1.to(10) res15: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 to 10 res16: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Scala does not have ++ or -- operators.
Calling Functions and Methods
Functions and methods are different in Scala. Like
scala> import scala.math._ // In Scala, the _ character is a “wildcard,” like * in Java import scala.math._ scala> pow(2, 4) res17: Double = 16.0
Or
scala> scala.math.pow(2,4) res19: Double = 16.0
Scala doesn’t have static methods, but it has a similar feature, called singleton objects. For example the BigInt companion object to the BigInt class has a method probablePrime that generates a random prime number with a given number of bits:
scala> BigInt.probablePrime(100, scala.util.Random) res20: scala.math.BigInt = 1091747225364644886626570445631
Scala methods without parameters often don’t use parentheses. For example,
scala> "Hello".distinct res21: String = Helo
The apply Method
scala> "Hello"(4)//the 5th element, equal to "Hello".apply(4) res24: Char = o scala> BigInt("1234567890")//equal to BigInt.apply("1234567890") res26: scala.math.BigInt = 1234567890
The apply method is not a member of Int, Double, Float, Short, Long, but a member of Char, String, Big**