• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() method and ==

 
mi te
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
mambe nanje
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Tilo Hemp
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kotto Bass
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
Vijay Raj
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
ak pillai
author
Ranch Hand
Posts: 288
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic