Object Equality in Scala

Scala | Object Equality: Here, we will learn about object equality with examples using different methods for equality.
Submitted by Shivang Yadav, on August 01, 2020

Objects in Scala are an entity with has state (data) and behavior (function performing on data).

In object-oriented programming like Scala, objects are very important part as everything is an object. And the comparison of objects is common to check for their identity. To check for equality of objects, the inbuilt methods available are:

  1. == and != method (operator)
  2. equals method
  3. ne and eq method

1) == and != operator

To check whether two objects are equal, we have the == operator in Scala. Also, there is a != operator with is negation of == operator.

Program:

case class employee (empName : String , salary : Integer)

object MyClass {
    def main(args: Array[String]) {
        var emp1 = employee("Prem", 140000)
        var emp2 = employee("Anshu", 200000)
        var emp3 = employee("Prem", 140000)
        var emp4 = emp1

        println("Example of == method")
        println(emp1 == emp2)
        println(emp1 == emp3)
        println(emp1 == emp4)

        println("Example of != method")
        println(emp1 != emp2)
        println(emp1 != emp3)
        println(emp1 != emp4)
    }
}

Output:

Example of == method
false
true
true
Example of != method
true
false
false

In the above code, we have a class employee that stores empName (storing employee name) and salary (i hope mine could be that much). For this class, we have created 4 objects. Then we have compared using == method and obtained these results.

emp1 == emp2, returns false. Both objects have different values.  

emp1 == emp3, returns true. The values of both objects are the same.

emp1 == emp4, returns true. Both objects refer to the same memory block.

We have also, compared the negation of == operator which is != operator and the results were obviously negations of the one which we got in the case of == operator.

2) equals() method

The equals() method is the same as == operator. It is used to check if the two operators are equal or not.

Syntax:

object1.equals(object2)

Program:

case class employee (empName : String , salary : Integer)

object MyClass {
    def main(args: Array[String]) {
        var emp1 = employee("Prem", 140000)
        var emp2 = employee("Anshu", 200000)
        var emp3 = employee("Prem", 140000)
        var emp4 = emp1

        println("Example of equals method")
        println(emp1.equals(emp2))
        println(emp1.equals(emp3))
        println(emp1.equals(emp4))
    }
}

Output:

Example of equals method
false
true
true

In the above code, we have illustrated the working of equals() method in scala. We have a class employee that stores empName (storing employee name) and salary (i hope mine could be that much). For this class, we have created 4 objects. For comparing these objects we have used the equals() method:

emp1.equals(emp2), returns false. Both objects have different values.

emp1.equals(emp3), returns true. The values of both objects are the same.

emp1.equals(emp4), returns true. Both objects refer to the same memory block.

3) ne() and eq() method

The ne() and eq() methods in Scala are used to check if the two objects are referencing the same object location i.e. for two objects obj1 and obj2, the eq() method returns true if and only if both refer to the same memory location. If the values of two objects are the same but they don't refer to the same object, the eq() method returns false.

Syntax:

For eq() method:
	object1.eq(object2)
For ne() method:
	object1.ne(object2)

Program:

case class employee (empName : String , salary : Integer)

object MyClass {
    def main(args: Array[String]) {
        var emp1 = employee("Prem", 140000)
        var emp2 = employee("Anshu", 200000)
        var emp3 = employee("Prem", 140000)
        var emp4 = emp1
        
        println("Example of eq method")
        println(emp1.eq(emp2))
        println(emp1.eq(emp3))
        println(emp1.eq(emp4))
        
        println("Example of ne method")
        println(emp1.ne(emp2))
        println(emp1.ne(emp3))
        println(emp1.ne(emp4))
    }
}

Output:

Example of eq method
false
false
true
Example of ne method
true
true
false

In the above code, we have illustrated the working of eq() and ne() methods in scala. We have a class employee that stores empName (storing employee name) and salary (i hope mine could be that much). For this class, we have created 4 objects. For comparing the equality of these objects we have used the eq() method:

emp1.eq(emp2), returns false. Both objects have different values.

emp1.eq(emp3), returns false. The values of both objects are the same but they don't refer to the same memory block.

emp1.eq(emp4), returns true. Both objects refer to the same memory block.

We have also, compared the negations of equality of the objects using the ne() method.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.