case class in Scala

Case class is special type of class. You can class by prepending case keyword in class declaration.

case class Person(id:Int, name: String)

Object Creation:

You do not need to use new keyword to create object of case class. 

object TestMe {
  def main(args: Array[String]): Unit = {
    val person = Person(1,"Prithvi")
  }
}

When you create case class, compiler generates apply method for constructor. When you create object, it internally calls apply method.

Visibility of variables:

If we don't specify visibility(var or val) in variable declaration, It will use val as by default. Since variable are declared as val, accessors will be generated for those variables but not mutators.  

Case classes are meant for immutability. Once object of case class is created, it should not be mutated. But we can make case class mutable by declaring variables as var but it will violate the intention of case class 

Boilerplate codes:

As we have seen, case class is one liner class. This is very simple class with name of class and its variable list. If case class is that simple then why are we discussing here.
Actually compiler generate lots of boilerplate code for case class. case class will have 22 more methods than regular class

lets compile this class using scalac command and decompile .class file

scalac Person.scala

Above command will generate below files -
Person.class
Person$.class

Now decomple Person.class file with below command

javap Person.class

Above command will generate below code - 

Compiled from "Person.scala"
public class Person implements scala.Product,scala.Serializable {
  public static scala.Option<scala.Tuple2<java.lang.Object, 
    java.lang.String>> unapply(Person);
  public static Person apply(int, java.lang.String);
  public static scala.Function1<scala.Tuple2<java.lang.Object,
    java.lang.String>, Person> tupled();
  public static scala.Function1<java.lang.Object,
    scala.Function1<java.lang.String, Person>> curried();
  public int id();
  public java.lang.String name();
  public Person copy(int, java.lang.String);
  public int copy$default$1();
  public java.lang.String copy$default$2();
  public java.lang.String productPrefix();
  public int productArity();
  public java.lang.Object productElement(int);
  public scala.collection.Iterator<java.lang.Object> productIterator();
  public boolean canEqual(java.lang.Object);
  public int hashCode();
  public java.lang.String toString();
  public boolean equals(java.lang.Object);
  public Person(int, java.lang.String);
}

As you can see case class will generate all above methods.
Lets discuss some of above methods.

toString()

Case class generates very nice toString() method. Let us run below code to see output of toString() method

object TestMe {
  def main(args: Array[String]): Unit = {
    val person = Person(1,"Prithvi")
    println(person)
  }
}
Output: Person(1,Prithvi)

equals()

Case class generates equals() method to compare object content. We don't not need to call equals method. We can use == to check objects are equal or not. Let's see below program

object TestMe {
  def main(args: Array[String]): Unit = {
    val person1 = Person(1,"Prithvi")
    val person2 = Person(1,"Prithvi")
    println(person1 == person2)
  }
}
Output: true

As you can see in above code, we have 2 objects of Person class. we used == to check equality and returns true.

Comments

  1. Nice blog post your gave for us. I heartfully thank you and i request you to add more informations like this in future.
    I have to appreciate you for your great work which you had done in your blog.i want you to add more like this.
    JAVA Training in Chennai
    JAVA Course in Chennai
    Digital Marketing Course in Chennai
    Python Training in Chennai
    Big data training in chennai
    Selenium Training in Chennai
    JAVA Training in Chennai
    JAVA Course in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java