aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() method and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() method and ==" Watch "equals() method and ==" New topic

equals() method and ==

mi te

Joined: Mar 09, 2006
Posts: 29
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 == e2);

class EqualsTest{
String s;

EqualsTest(String x){
s = x;

Why am I getting false for both of them?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In the EqualsTest class, you inherit the equals methods from 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

Joined: Feb 22, 2006
Posts: 31
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

Da Clone in programming world
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
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).

[ March 14, 2006: Message edited by: Tilo Hemp ]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11411

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kotto Bass

Joined: Jan 27, 2006
Posts: 17
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

Joined: Oct 10, 2005
Posts: 110
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,


ak pillai
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.

java j2ee job interview questions with answers | Learn the core concepts and the key areas
wood burning stoves
subject: equals() method and ==