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 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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Differnce between == and equals" Watch "Differnce between == and equals" New topic
Author

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


http://www.skillfox.com Home of Java Skills SCJP 5.0, SCBCD, SCEA mock exams
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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;
}
Bill
 
wood burning stoves
 
subject: Differnce between == and equals