• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() to check equality of two objects

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an Employee object like
Employee emp=new Employee(String,int)
How to check equality of two Employee objects.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use equals() method
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through following link.
Hope that will help you.

Java World
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic