Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals().....

 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the same vein, if everything in Java is a class then why are primitives left out.
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic