Comparing Objects in Java
Author: Van Glass, vglass@jfind.com
|
| |
|
One question I frequently see is "How do I compare two objects to see if they are equal?" The answer
to this question is rather simple but is often misunderstood. Every class by default has a method called
equals which has the following signature:
public boolean equals(Object obj)
This method accepts an object as its argument, performs a comparison and returns true if the two object instances are equal and false otherwise.
What some developers fail to realize is that the equals method in its default state only compares the memory addresses of two objects, not their contents. To compare
the contents of two objects we must override the equals method.
For example.
|
public class Student
{
private int student_id;
public Student(int student_id)
{
this.student_id = student_id;
}
public static void main(String[] args)
{
Student s1 = new Student(8888);
Student s2 = new Student(8888);
if(s1.equals(s2))
System.out.println("They match!");
else
System.out.println("They do not match!");
}
}
|
In the example above although the Student objects are created with the same
student_id in the constructor the main method will print out "They do not match!" to the
console. The reason behind this is that we did not override the equals method. By not overriding
the equals method the virtual machine looks at the memory addresses of each object and compares the
two, in this case returning false. This is not the behavior we want. We really want to compare the
contents of each Student object.
To fix this problem we should override the equals method for the Student class as follows.
|
public class Student
{
private int student_id;
public Student(int student_id)
{
this.student_id = student_id;
}
public boolean equals(Object obj)
{
if(obj == this)
return true;
/* is obj reference null */
if(obj == null)
return false;
/* Make sure references are of same type */
if(!(getClass() == obj.getClass()))
return false;
else
{
Student tmp = (Student)obj;
if(tmp.student_id == this.student_id)
return true;
else
return false;
}
}
public static void main(String[] args)
{
Student s1 = new Student(8888);
Student s2 = new Student(8888);
if(s1.equals(s2))
System.out.println("They match!");
else
System.out.println("They do not match!");
}
}
|
Now the new class definition overrides the equals method and checks a couple things
to see if they two object instances are really equal.
1. Checks to see if obj is an instanceof Student. If not we can automatically assume that
they are not equal.
2. We check to see if the member variable student_id are the same in both instances.
In this case the two Student instances are equal and the text "They match!" will be printed to
the console.
|
| Sponsored Links - please visit our sponsors |
| Java FTP Component | | Easily add FTP to your Java apps. | | http://www.jscape.com/inetfactory/ftp.html |
| Java SMTP Component | | Easily add SMTP to your Java apps | | http://www.jscape.com/inetfactory/smtp.html |
| Learn Java Server Faces! | | Use WebGalileo Faces JSF components to quickly create Java based web apps. | | http://www.jscape.com/webgalileofaces/ |
| Secure FTP Applet | | Connect to FTP securely from within your browser. | | http://www.jscape.com/sftpapplet/ |
Sponsor this site
| |