Monday, April 8, 2013

Equal assignment operator vs clone()


Equal assignment operator

When we use assignment operator to assign one object to another a new object does not get created. Both the objects point to same reference. Only reference gets copied. Changes in any object will change the object reference and will be reflected for the other one. Comparison using (==) operator will return TRUE since both points to same reference. Needless to mention equal() also returns TRUE.
Default hashCode() of super class will return same result for both.

package com.deva;

public class Student implements Cloneable {
      
       private int age;
       private String name;
       private String dept;
       public String getDept() {
              return dept;
       }
       public void setDept(String dept) {
              this.dept = dept;
       }
       public int getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      
       public static void main(String[] args) throws CloneNotSupportedException {
             
              Student student =new Student();
              student.age=25;
              student.name="deva";
              student.dept="CS";        
              Student student2 = student;
              student2.age = 35;
             
              System.out.println("Student1 detail "+student);
              System.out.println("Student2 detail "+student2);
              System.out.println("Student1 hashcode="+student.hashCode());
              System.out.println("Student2 hashcode="+student2.hashCode());
              System.out.println("== operator returns "+(student==student2));
              System.out.println("equals method returns "+(student.equals(student2)));         
                                 
       }
      
       @Override
       public String toString() {

              String s = "Age="+this.age+"  Name="+this.name+"  Dept="+this.dept;
              return s;
       }
}


O/P:-
Student1 detail Age=35  Name=deva  Dept=CS
Student2 detail Age=35  Name=deva  Dept=CS
Student1 hashcode=1523239031
Student2 hashcode=1523239031
== operator returns true
equals method returns true


Creation of Object using clone()

In case when we create an object using clone() method a separate object gets created with a different reference but with same properties. Changes to one object will not be reflected for other one since both the object has different reference. Comparison using (==) operator will return FALSE.
Default equals() method of parent class will also return FALSE and hashCode() of super class will return different result for both.

package com.deva;

public class Student implements Cloneable {
      
       private int age;
       private String name;
       private String dept;
       public String getDept() {
              return dept;
       }
       public void setDept(String dept) {
              this.dept = dept;
       }
       public int getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      
       public static void main(String[] args) throws CloneNotSupportedException {
             
              Student student =new Student();
              student.age=25;
              student.name="deva";
              student.dept="CS";        
              Student student2 = (Student) student.clone();
              student2.age = 35;
             
              System.out.println("Student1 detail "+student);
              System.out.println("Student2 detail "+student2);
              System.out.println("Student1 hashcode="+student.hashCode());
              System.out.println("Student2 hashcode="+student2.hashCode());
              System.out.println("== operator returns "+(student==student2));
              System.out.println("equals method returns "+(student.equals(student2)));         
                                 
       }
      
       @Override
       public String toString() {

              String s = "Age="+this.age+"  Name="+this.name+"  Dept="+this.dept;
              return s;
       }
}


O/P:-
Student1 detail Age=25  Name=deva  Dept=CS
Student2 detail Age=35  Name=deva  Dept=CS
Student1 hashcode=925838130
Student2 hashcode=2042428395
== operator returns false
equals method returns false

No comments:

Post a Comment