File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 13037
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
It's not a secret anymore!