File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes equals()..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals()....." Watch "equals()....." New topic
Author

equals().....

Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
Here is a replication of code that appears on pg 104 of 'thinking in java'
public class EqualsMethod{
public static void main(String[] args){
Integer n1 = Integer(47);
Integer n2 = Integer(47);
System.out.println(n1.equals(n2));
}
}
The result of the above will be true.

class Value{
int i;
}
public class EqualsMethod2{
public static void main(String[] args){
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
}
the result of the above is false

I fail to understand how the second result is false. Could someone kindly explain.

THANKS.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
Because the Value-class doesn't redifine the equals-method, which means that it gets the default equals() from Object. This one returns true if the two references used are the same, i.e. refers to the same instance of an object. Here however you have to different instances of Value.
Does that answer it?
/Mike
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You also have to understand why the first was equal. The first Integer class was true because the equals() method in Object was overridden to check the value that it wraps. The second example Value class used the method equals() that it inherited from Object. That method just does a == comparison which for Objects checks to see if it is the same Object. In the second example they where not the same.
Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Also note that if equals() is not overrridden for some Object, Objects of that class are compared with a simple '==', ie whether they are the same object, not whether they contain equal members.


Sun Certified Programmer for the Java 2 Platform 1.4
Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
Thank you Mikael, Paul & Michael....things are clear now.


[This message has been edited by Rajendra Deshpande (edited July 03, 2001).]
Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
In the same vein, if everything in Java is a class then why are primitives left out.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
To make it easier to handle. Of course they COULD have removed them and made everything classes, but that would give the code unnecesary complexity. Plus, you need something to build the classes on.
/Mike
Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
Mikael,
Thanks for your quick reply. In lighter vein....primitive classes could also be called 'previliged classes' for occupying a place on the stack and not on the heap...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals().....
 
Similar Threads
objects
Object comparison
comparing objects
equals method .. comparing objects
Doubt on equals()