This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Differnce between == and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Differnce between == and equals" Watch "Differnce between == and equals" New topic

Differnce between == and equals

anil kuchana
Ranch Hand

Joined: Sep 08, 2000
Posts: 404
hello every1,
please state the difference between == operator and equals method Home of Java Skills SCJP 5.0, SCBCD, SCEA mock exams
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
It is absolutely essential to know this one.
The == operator with reference types (ie Objects) evaluates as true if the references are identical - point to the same thing.
With value types (ie primatives) it evaluates as true if the value is identical.
The purpose of the equals method is to return true if two objects have identical content - however, the equals method in the java.lang.Object class - the default method if a class does not override it - returns true only if both references point to the same thing.
In order to be really useful, classes typically override the equals method to test for equality of content. Look at the source code for the equals method in the java.lang.Long class for instance.
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof Long)) {
return value == ((Long)obj).longValue();
return false;
I agree. Here's the link:
subject: Differnce between == and equals
Similar Threads
what is the difference between == and .equals() method????
difference between equals() and == and compareTo()
confusion between == & equals( )
Significant different between == and equals()?
loop trouble