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 equals question 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 "equals question" Watch "equals question" New topic

equals question

Bob Vel

Joined: Mar 21, 2001
Posts: 24
public class EqualsClass1 {
public static void main(String[] args) {
Integer i1 = new Integer(50);
Integer i2 = new Integer(50);
Result is true - Perfect

class Value {
int i;
public class EqualsClass2 {
public static void main(String[] args) {
Value b1 = new Value();
Value b2 = new Value();
b1.i = b2.i = 75;
Result is false - How come?
Please explain. Thanks in advance.

Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Bob,
The wrapper classes (Boolean, Byte, Short, Integr, etc) override Object.equals() to return true if (a)the objects are of the same type and (b)they contain the same values. Which is why your first example returns 'true'. i1 and i1 are of the same type, Integer, and contain the same value, 50.
In the second example, your class Value does not override Object.equals(). The default behaviour provided in Object.equals() only returns true if both reference variables point to the same object in memory i.e. for b1.equals(b2) to be true, b1 == b2 must be true. In your example they are not. b1 and b2 point to two different Value objects in memory.
For more info, check the API for the Object.equals() method and the equals() methods in the Integer class.
Hope that helps.

Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Jane's explanation is very sufficient.
I just wanted to add 2 cents to it
Basically every other class extends Object so they all get
equals() by default. However default equals() are shallow (ie
== is what happenin) .
To make them deep copying you need to override it
and thats what happenin in Wrapper and String classes
Harsha Jay
Ranch Hand

Joined: Jul 23, 2001
Posts: 177
I am bit confused .... then suppose variable i in class value is now declared in EqualsClass2 and same process is followed
Would the result change???
Pls Explain
Harsha Jay
Ranch Hand

Joined: Jul 23, 2001
Posts: 177
What i meant was this
public class EqualsClass2 {
int i;
public static void main(String[] args) {
EqualsClass2 b1 = new EqualsClass2();
EqualsClass2 b2 = new EqualsClass2();
b1.i = b2.i = 75;

Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Here issue is the same, every class implicitly inherits equals() method, which if not overrided does not perform deep comparison, so the reult will be false.

Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The default equals() method only compares if two pointers are pointing to the same object. It does not compare the values of anything inside the object.
In order for your example to work, you must supply an:
equals(Object o) method.
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
You need to override the equals method in the Value class to get the kind of result you expect as with the Integer.
For all the wrapper classes, the equals method has been overriden to compare the contents of the value it holds.
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
I agree. Here's the link:
subject: equals question
Similar Threads
Doubt in equals and == . Please help.
Boolean values
wrapper Boolean class problem
wrapper classes
== Vs .equals()