Wednesday, June 26, 2013

1)What is equals method and why is it important ?


In java every class implicitly extends Object class.So,when you write a class, it has equals
method automatically even if you haven't written equals method.(The equals method in
Object class will be inherited in to Student class).

Copy the following code in blue into Student.java,compile and execute it.
Go through the comments section in the code.I have explained clearly... why the
following code returns that objects are not equal.


class Student{
     int studentId;//This is the only property of Student

   

     public static void main(String args[]){

        //Create one student object with name first

        Student first = new Student();
        first.studentId = 100;
       
        //Create one student object with name second
        Student second = new Student();
        second.studentId = 100;
        /*In student class I haven't written equals method but still I am able to use it
        because as I said earlier every class inherits Object class,so we are able to use equals
        method which is inherited from Object class.
       
         Following code prints........ "First Student and Second Student are not equal"
         Logically as two students are having same student id, they should be equal,
        but the equals method from Object class checks if the memory references are
        same or not,it doesn't check if student ids in the two objects are same or not.
       
         I will explain you in more detail what do I mean comparing memory references.
        When you say Student first = new Student(); in memory student object is created and
        reference to that object is assigned to first.This reference is unique for every object.
       
        Student first = new Student();
        above statement creates student object in memory and reference to the object created
        (This will be always unique for every object)is assigned to first
       (For example assume reference returned is 1000)
       
        Student first = new Student();
        above statement creates student object in memory and reference to the object created
        (This will be always unique for every object) is assigned to second
       (For example assume reference returned is 2000 and assigned to second)
       
        equals method inherited from object method checks if references are equal
        So in our example it checks if 1000 == 2000, as they are not equal,equals
        method returns false*/
       
       
        if(first.equals(second)){
             System.out.println("First Student and Second Student are equal");
        }else{
             System.out.println("First Student and Second Student are not equal");
        }
    }   
 }

Output:
--------

C:\test>java Student
First Student and Second Student are not equal

2)How to modify the above class to compare students based on student ids and not based on memory references ?

Solution is you have to override the equals method... in Student class.
Overriding means you create equals method in Student class which has same 
signature as the method inherited from Object class.

Add the equals method given below(highligted in red) into Student class and
compile again,run and see the output.

Now it will print "First Student and Second Student are equal".
Because now equals method in student checks if the studentIds are same
if student Id's are same(100 == 100) it return true otherwise false.


 /*Define equals method in Student class which overrides
 equals method inherited from Object class*/
     

   
    public boolean equals(Object st){
        if(st instanceof Student){
          
           Student second =(Student)st;
           if(this.studentId == second.studentId){
                return true;
           }else{
               return false;
           }
        }else{
             return false;
        }
       }




Hope you understood......
  











No comments:

Post a Comment