my dog learned polymorphism
The moose likes Beginning Java and the fly likes equals versus == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals versus ==" Watch "equals versus ==" New topic

equals versus ==

sindura kapur

Joined: Jul 14, 2008
Posts: 22

class Dog
Dog d1=new Dog();
Dog d2=new Dog();
boolean b1=(d1==d2);
boolean b2=(d1.equals(d2));System.out.println(b1,b2);

what will be the values of b1 and b2
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Both will be false.

== will check if two references point to the same object. For instance, if you take a third variable and assign the value of d1 to it (Dog d3 = d1) , then d1 == d3.

equals will use some developer defined comparison. For Strings it is whether or not both Strings have the exact same characters, for Integers it is whether they represent the same int value, but this comparison can easily include many many fields.

However, you as a developer will have to override equals yourself. If you do not, you will inherit the implementation of your parent class. For Dog, that is Object, and Object simply uses == in its check:

Please read the API of Object.equals and Object.hashCode for the exact rules.
[ July 27, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Questions on equals() versus == get asked a lot. You can take a look at this recent thread for a detailed discussion on the subject. And if you search the forum, you'll find a number of other discussions on it.
Agarwal Priyanka

Joined: Jul 25, 2008
Posts: 20
all the primitive type wrapper classes override the Object.equals() method to compare the value of the objects; the default Object.equals() checks if the variables reference the same object.
equals(Object anObject)Compares this string to the specified object


str1 == str2 : true
str2 == str3 : true
str3 == str1 : true
str4 == str5 : false
str1 == str4 : false
str1 == str5 : false
str1.equals(str2) : true
str2.equals(str3) : true
str3.equals(str1) : true
str4.equals(str5) : true
str1.equals(str4) : true
str1.equals(str5) : true

keep trying...thats the way to success...<br />with best regards...<br />p.agarwal
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

Understanding equals is an important aspect in Java. Refer to the article here.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
equals you test the values and == you test reference

Brazil - Sun Certified Java Programmer - SCJP 5 About Java - Update every Week.
Guide SCJP - tips that you need know
sindura kapur

Joined: Jul 14, 2008
Posts: 22
when i am compiling this i am getting :reached end of file while parsing??what should i do
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

That message means that the compiler was reading your file and was expecting more. It's usually a sign of unbalanced braces. Make sure all your opening braces are closed.
I agree. Here's the link:
subject: equals versus ==
It's not a secret anymore!