Scala – Case Class

  • date 29th December, 2020 |
  • by Prwatech |
  • 0 Comments

Scala Case Classes and Case Object

 

A Case Class is actually similar to an ordinary class, which has a component for displaying unchangeable data. It is likewise valuable in pattern matching. It has been characterized with a modifier case, because of this case catchphrase, we can get a few advantages to prevent oneself from completing an areas of codes that must be remembered for some spots with practically zero change. As we can see under an insignificant case class needs the keyword case class, an identifier, and a parameter list which might be empty.

Case classes and case objects are powerful constructs in Scala for defining data structures and modeling immutable entities. They offer concise syntax for defining immutable data with built-in support for pattern matching and structural equality.

Case Classes:

  • Case classes are regular classes with additional compiler-generated methods, making them ideal for representing immutable data.
  • They automatically generate methods for object construction, pattern matching, equality checks (equals), and hash code generation (hashCode).
  • Case classes support pattern matching, enabling concise and readable code for deconstructing and matching data structures.

Syntax:

Case class className(parameters)

Use case 1:

     // Scala program of case class and case Object
     case class course(name:String, dur:String)
     object prwatech {
     // Main method
     def main(args: Array[String])
     {
     var c = course("BigData", "4 month" )
    // Display both Parameter 
    println("Name of the course is " + c.name); 
    println("Duration of the course is " + c.dur); 
     } 
    }

OUTPUT:

Name of the course is BigData
Duration of the course is 4 month

Use case 2:

    
// Scala program of case class and case Object
case class Student(name:String, age:Int)
object prwatech {
// Main method
def main(args: Array[String])
{
var a = Student("Sandeep", 25 )
// Display both Parameter 
println("Name of the Student is " + a.name); 
println("Age of the Student is " + a.age); 
} 
}

output:

Name of the Student is Sandeep
Age of the Student is 25

Use case 3:

// Scala program of case class and case Object
// affix a method with the name of the class
case class Course (name:String, Language:String)
object prwatech
{
// Main method
def main(args: Array[String])
{
var Course1 = Course("Data Science", "Python")
var Course2 = Course("Big Data", "Spark Sql") 
   // Display strings
    println("Name of the Course1 is " + Course1.name);
    println("Language of the Course1 is " + Course1.Language);
    println("Name of the Course2 is " + Course2.name);
    println("Language of the Course2 is " + Course2.Language);
}
}

output:

Name of the Course1 is Data Science
Language of the Course1 is Python
Name of the Course2 is Big Data
Language of the Course2 is Spark Sql

Scala Case Classes and Case Object

Quick Support

image image