Professional Documents
Culture Documents
Scala 11 14
Scala 11 14
• Apply() method: Assembling an object from its components is done with this
method. For example, by using the two components firstName and lastName,
we can create the Employee object with the apply method.
• Unapply() method: This method follows the reverse order of apply order and
decomposes an object into components. For example, you can break or
decompose that employee object down into its components i.e., firstName and
lastName.
Syntax:
Example:
import scala.collection.immutable.BitSet
object InterviewBit
{
def main(args:Array[String])
{
println("Initialize a BitSet")
val numbers: BitSet = BitSet(5, 6, 7, 8)
println(s"Elements of BitSet are = $bitSet")
println(s"Element 6 = ${bitSet(6)}")
println(s"Element 3 = ${bitSet(3)}")
println(s"Element 7 = ${bitSet(7)}")
}
Output:
Initialize a BitSet
Elements of BitSet are = BitSet(5,6,7,8)
Element 6 = true
Element 3 = false
Element 7 = true
4.5
100K+
Play Store
Scala provides us with a function called ofDim that declares multidimensional arrays
using the matrix format. Array.ofDim can be used to declare multidimensional
arrays. There's no limit to the number of dimensional arrays you can create.
Syntax:
or
Example:
object MultiArrayExample
{
def main(args: Array[String])
{ val multiArr= Array.ofDim[Int](2, 2)
multiArr(0)(0) = 5
multiArr(0)(1) = 10
multiArr(1)(0) = 15
multiArr(1)(1) = 20
for(i <- 0 to 1; j <- 0 to 1)
{
println("Element "+ i + j + " = " + multiArr(i)(j))
}
}
}
Output:
Element 00 = 5
Element 01 = 10
Element 10 = 15
Element 11 = 20
• Fields: Variables of this type can be accessed from any method within a Web
object, or from outside the object, depending on access modifiers used.
Depending on the var and val keywords, they can be mutable or immutable.
• Method Parameters: When a method is invoked, these variables are used to
pass values to the method. All method parameters use the val keyword and are
strictly immutable. Normally, these are accessed within a method, but a
Reference allows accessing them outside the method.
• Local Variables: These are the variables (mutable or immutable) declared inside
a method and accessible only within that method. By returning them from the
method, they can be accessed outside of the method.
Map() and its close cousin flatMap() are often used when we deal with data
structures in Scala and both are higher-order functions.