Scala | Value Classes

Value classes are a new mechanism which help to avoid allocating run time objects. AnyVal define value classes. Value classes are predefined, they coincide to the primitive kind of Java-like languages.
There are nine predefined value types : Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean.

equals or hashCode cannot redefine by a value class. Value classes are mainly used to optimize performance and memory.

Let’s understand value classes with some example.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate value class
  
// Creating a value class and extend with AnyVal
case class C(val name: String) extends AnyVal
  
// Creating object
object gfg
{
    // Main method
    def main (args: Array[String])
    {
        // Creating the instance of the ValueClass
        val c = new C("GeeksForGeeks")
        c match 
        {   
            // new C instantiated here
            case C("GeeksForGeeks") => println("Matched with GeeksForGeeks")
            case C(x) => println("Not matched with GeeksForGeeks")
        }
    }
}

chevron_right


Output:

Matched with GeeksForGeeks

In the above code, a value class defined with the help of case class, Here AnyVal define value class(C). value class consist one string parameter. When we will pass same string as in case statement then this will return true otherwise false.

 
Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate value class
  
// Creating a value class and extend with AnyVal
class Vclass(val a: Int) extends AnyVal 
{
    // Defining method
    def square() = a*a
}
  
// Creating object
object gfg
{
    // Main method
    def main (args: Array[String])
    {
        // creating the instance of the ValueClass
        val v = new Vclass(5)
        println(v.square())
    }
}

chevron_right


Output:

25

As we can see, in above example, a value class created and representation is an int. The above code consist a def in the value class Vclass. Here Vclass is a user-defined value class that wraps the Int parameter and encapsulates a square method. To call the square method, create the object of the Vclass class as like: val v = new Vclass(5)

Some Restrictions of Value Classes –

  • A value class might not have specialized type parameters. may not have specialized type parameters.
  • A value class may not have nested or local classes, traits, or objects.
  • equals or hashCode cannot redefine by a value class.
  • A value class cannot have lazy vals, vars, or vals as members. It can only have defs as members.
  • No other class can extend a value class.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.