0 votes
in Scala Constructs by
What are case classes in Scala?

1 Answer

0 votes
by

Scala case classes are like regular classes except for the fact that they are good for modeling immutable data and serve as useful in pattern matching. Case classes include public and immutable parameters by default. These classes support pattern matching, which makes it easier to write logical code.  The following are some of the characteristics of a Scala case class: 

Instances of the class can be created without the new keyword.

As part of the case classes, Scala automatically generates methods such as equals(), hashcode(), and toString().

Scala generates accessor methods for all constructor arguments for a case class.

Syntax:

 case class className(parameters)

Example: 

 case class Student(name:String, age:Int)    

object MainObject 

{                                                                                             

    def main(args:Array[String]) 

    {   

        var c = Student(“Aarav”, 23)                                    

        println("Student name:" + c.name);                    

        println("Student age: " + c.age);   

    }   

}   

Output:

Student Name: Aarav 

Student Age: 23 

Related questions

0 votes
asked Sep 12, 2022 in Scala Constructs by Robin
0 votes
asked Sep 11, 2022 in Scala Constructs by sharadyadav1986
...