wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes equals() to check equality of two objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() to check equality of two objects" Watch "equals() to check equality of two objects" New topic
Author

equals() to check equality of two objects

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
I have an Employee object like
Employee emp=new Employee(String,int)
How to check equality of two Employee objects.
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
depends on how you have modeled the employee object. For instance you assume the employee no would uniquely identify an employee,
then you can compare employee id of the two objects to see if they are same.
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

use equals() method


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
Dear Ajay,
Each employee should have unique ename i.e. String and unique ecode i.e. int.
Here is the Employee class:

public class Employee {
int ecode;
String ename;
public Employee(String ename,int ecode)
{
this.ename=ename;
this.ecode=ecode;
}
public String toString()
{
return "Employee name is >>"+this.ename+"Employee code is>> "+this.ecode;
}
public boolean equals(final Object obj)
{
// code to test equality
}
}

what should I write in equals()?
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by Gurudhaasan:
use equals() method

Gurudhaasan, you are (as in the "programmer,developr" post) incorrect. The
equals method tests if two objects are the same.. but in the case with
employees they are the same if they have the same value. Please consider
your postings.. and remember to state if your not 100% sure that your
answer is correct.



vijayk gopu: Each employee should have unique ename i.e. String and unique ecode
vijayk gopu: what should I write in equals()?


You have actually answered your own question if a unique employee id
identified by a a unique String and a unique integer, how would you
implement an equalily check, i.e. when two objects are identical (have the
same values) ?

Happy coding

/Svend Rost, who'll check back later
Shashi Kant
Ranch Hand

Joined: Aug 08, 2006
Posts: 117
Go through following link.
Hope that will help you.

Java World


If it's true that we are here to help others, then what exactly are the others here for?
vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
Dear Svend,

Thanks for the reply.

if a unique employee id
identified by a a unique String and a unique integer, how would you
implement an equalily check, i.e. when two objects are identical (have the
same values) ?


I am writing a code in JUnit where I need to check for equality of an existing object say Employee("aa",1111) with some external object with same ename and ecode i.e. "aa" and 1111 respectively.
How to acomplish this?

So If I have to call obj.equals(existing Employe object in an Hashtable)
what should I write in equals() method?
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by vijayk gopu:

I am writing a code in JUnit where I need to check for equality of an existing object say Employee("aa",1111) with some external object with same ename and ecode i.e. "aa" and 1111 respectively.
How to acomplish this?

So If I have to call obj.equals(existing Employe object in an Hashtable)
what should I write in equals() method?


Okay, two objects (employees) are equal if they have/are in the same state,
i.e. their variables (ename and ecode) have identical values.

Let's look at it shall we...


Does this help? try to implement it.. if you run into problems, tell me
what the problem is and how you could imagine you should solve it..

/Svend Rost, who'll check back later
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
If your intent is to override the equals() method in Object class the method signiture must be:


Note that if you override equals() then you should override hashCode(). Look for examples of well written equals() and hashCode() here at javaranch.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equals() to check equality of two objects