This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma 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

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

equals and ==

Abhi Kumar

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.

Alexander Arenas

Joined: Feb 24, 2009
Posts: 8

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

!!! RockStarArtist !!!
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1323

Topic already being discussed on this thread.

[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 About Java - Update every Week.
Guide SCJP - tips that you need know
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

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.

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

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

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

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:
subject: equals and ==
It's not a secret anymore!