This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals versus ==" Watch "equals versus ==" New topic
Author

equals versus ==

sindura kapur
Greenhorn

Joined: Jul 14, 2008
Posts: 22
suppose

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
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
Greenhorn

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



Output

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: 9353
    
    2

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
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
sindura kapur
Greenhorn

Joined: Jul 14, 2008
Posts: 22
when i am compiling this Strings1.java 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: http://aspose.com/file-tools
 
subject: equals versus ==