This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals and ==" Watch "equals and ==" New topic
Author

equals and ==

Abhi Kumar
Greenhorn

Joined: Sep 11, 2008
Posts: 24
Hi Gems,

I'm always confused between the equals and "==" .
Can anyone please let me know the exact difference.

Regards
Alexander Arenas
Greenhorn

Joined: Feb 24, 2009
Posts: 8
Hello!

This guy has a great definition on equals() vs ==

http://leepoint.net/notes-java/data/expressions/22compareobjects.html


!!! RockStarArtist !!!
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Topic already being discussed on this thread.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
normally you use equals to compare reference to objects and == when you want to compare values. for instance:


the result is true and false. with == if you are testing reference and no values.



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/
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Basically == tests to see if two references point to the same object on the heap, and .equals tests object equality either, by default (see for instance strings) or by your own implementation (ie. by overriding the .equals method).



However, take note you will have to read more about bothe equals and Strings, as strings have a partiular behaviour with equals and ==, especially when it comes to the String pool.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

equals method if for comparing values of two variables/objects and == for checking whether two objects are pointing to same memory location or not


Dhruva
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
'==' will always check whether the two reference variable point to the same object in the heap or not. If they do, it returns true.
Ex:

Cat obj1 = new Cat();
Cat obj2 = new Cat();
Cat obj3 = obj1;

obj1==obj2----------------------> always false as "new" creates two separate objects.
but
obj3==obj1-----------------------> true.


As far as equals() is concerned it checks the content of two objects.
Ex:

String s1 = new String("Test");
String s2 = new String("Test");


s1.equals(s2)-----------------> true since both have value "Test"
but s1==s2--------------------> false as they both refer to two separate objects.


For your own custom made classes like Cat, Dog, Horse, Vehicle you have to override the equals()to decide the criteria on which equals method returns true.
If you don't the default equals() simply checks for == for your custom made classes.


"History would be kind to me, for I intend to write it."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and ==