Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

equals() method and ==

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought equals() method compares the values in objects, and == operators checks the references to objects.

Please look the code below.

public class StringTest{

public static void main(String args[]){
EqualsTest e1 = new EqualsTest("hello");
EqualsTest e2 = new EqualsTest("hello");

System.out.println(e1.equals(e2));
System.out.println(e1 == e2);
}
}

class EqualsTest{
String s;

EqualsTest(String x){
s = x;
}
}

Why am I getting false for both of them?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the EqualsTest class, you inherit the equals methods from Object.java. The method that's defined there only checks references. If you want to compare two objects from a class that you define, you need to override equals and put your own definition of what it means for two objects of your type to be equal.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think u have to override the equals method be4 u can take control of the method in ur own classes that extend the Object class directly
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mi,

you don't overwrite the equals() method. the inherited implementation in Object just tests for reference equality.

you get false because you create two different objects (--> unequal references).

regards
[ March 14, 2006: Message edited by: Tilo Hemp ]
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the equals() method does whatever you tell it to do. it could, conceivably, do just about anything.

Because it is such an important method, the grand-daddy Object class has one defined. Every class, since they all derive from Object, automagically gets a copy of its equals() method, including your EqualsTest class. you get it free, without doing anything!!!

now, unfortunatly, the way it's defined in the Object class is to compare references. because really, there is no other default implementation that would work for any possible class that would ever be written by someone.

so, if you create your own class, and want equals() to make sense, you must override it and define it how you want it to work. many of the existing classes, such as String or Int, have already done this for you.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals() method will compare object references, unless it is overwridden, I think. For String literals, it will compare values (because it has been overwridden), while for wrapper classes, it will compare the values of their primitive values (because it has been overwridden as well).
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are overriding equals(), also try to override hashCode() method. If to objects are equals, then their hashcodes should also be equal. However unequal objects need not produce distinct hash codes.

For more information,

read

regards,
vijay.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A 16:You should override the equals() and hashCode() methods from the Object class. The default implementation of the equals() and hashcode(), which are inherited from the java.lang.Object uses an object instance�s memory location (e.g. MyObject@6c60f2ea). This can cause problems when two instances of the car objects have the same colour but the inherited equals() will return false because it uses the memory location, which is different for the two instances. Also the toString() method can be overridden to provide a proper string representation of your object. Points to consider:

--If a class overrides equals(), it must override hashCode().
--If 2 objects are equal, then their hashCode values must be equal as well.
--If a field is not used in equals(), then it must not be used in hashCode().
--If it is accessed often, hashCode() is a candidate for caching to enhance performance.
reply
    Bookmark Topic Watch Topic
  • New Topic