Wednesday, 20 December 2017

Introduction to Scala - VII

We continue with more Scala on the topic of Case Classes in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. In this post, we will look at the way Case Classes are declared and instantiated in programs.

A Case Class is defined by the below statement:

case class Employee()

The results are shown below:

scala> case class Employee()
defined class Employee


We can add parameters to the class definition. The parameters by default are val. We can then instantiate by merely calling it as shown below:

case class EmployeeDetails(Id: Int, FirstName: String, LastName: String)

val employee1 = EmployeeDetails(1000,"John","Doe")

val employee2 = EmployeeDetails(1000,"John","Doe")

val employee3 = EmployeeDetails(1001,"Foo","Bar")

The results are shown below:

scala> case class EmployeeDetails(Id: Int, FirstName: String, LastName: String)
defined class EmployeeDetails

scala> val employee1 = EmployeeDetails(1000,"John","Doe")
employee1: EmployeeDetails = EmployeeDetails(1000,John,Doe)

scala> val employee2 = EmployeeDetails(1000,"John","Doe")
employee2: EmployeeDetails = EmployeeDetails(1000,John,Doe)

scala> val employee3 = EmployeeDetails(1001,"Foo","Bar")
employee3: EmployeeDetails = EmployeeDetails(1001,Foo,Bar)


We can call the employee details as shown below:

scala> employee1.FirstName + " " + employee1.LastName
res5: String = John Doe


But, we cannot assign any value to any of the parameters as the parameters are val by default.

Comparison using equals returns results based on values in the parameters as shown below :

scala> employee1 equals employee1
res8: Boolean = true

scala> employee1 equals employee2
res9: Boolean = true

scala> employee2 equals employee3
res10: Boolean = false


Comparison using == returns results based on values in the parameters as shown below :

scala> employee1 == employee1
res11: Boolean = true

scala> employee1 == employee2
res12: Boolean = true

scala> employee2 == employee3
res13: Boolean = false


Comparison using != that is opposite of == returns results based on values in the parameters as shown below :

scala> employee1 != employee1
res17: Boolean = false

scala> employee1 != employee2
res18: Boolean = false

scala> employee2 != employee3
res19: Boolean = true


Comparison using eq returns results based on references of the objects as shown below :

scala> employee1 eq employee1
res20: Boolean = true

scala> employee1 eq employee2
res21: Boolean = false

scala> employee2 eq employee3
res22: Boolean = false


Comparison using ne that is opposite of eq returns results based on references of the objects  as shown below :

scala> employee1 ne employee1
res23: Boolean = false

scala> employee1 ne employee2
res24: Boolean = true

scala> employee2 ne employee3
res25: Boolean = true


To replicate new objects, we can use copy method. We can set new values too.

val employee4 = employee1.copy(Id=1002,FirstName=employee3.FirstName)

println(employee4)

The results are shown below:

scala> val employee4 = employee1.copy(Id=1002,FirstName=employee3.FirstName)
employee4: EmployeeDetails = EmployeeDetails(1002,Foo,Doe)

scala> println(employee4)
EmployeeDetails(1002,Foo,Doe)


This concludes the discussion on Case Classes in Scala.