• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on equals()

 
Xiao Song
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all. Why equals() can not treat class Value and Integer the same?


thanks!
[ September 07, 2006: Message edited by: Xiao Song ]
 
Carlos G�mez
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the Integer class override the equals method and certain primitives are always to be boxed into the same immutable wrapper objects. These special values are:

The boolean values true and false
The byte values
The char values in the range '\u0000' to '\u007F'
The short and int values between -128 and 127

for instance:

Integer j1 = 148;
Integer j2 = 148;
System.out.println(j1 == j2); // FALSE

Integer k1 = 126;
Integer k2 = 126;
System.out.println(k == k); // TRUE
 
Xiao Song
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think you Carlos,
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carlos Gomez:
Because the Integer class override the equals method and certain primitives are always to be boxed into the same immutable wrapper objects. These special values are:

The boolean values true and false
The byte values
The char values in the range '\u0000' to '\u007F'
The short and int values between -128 and 127

for instance:

Integer j1 = 148;
Integer j2 = 148;
System.out.println(j1 == j2); // FALSE

Integer k1 = 126;
Integer k2 = 126;
System.out.println(k == k); // TRUE


Carlos' reply is almost irelevant to the question - for two reasons:

a) Note that

Integer i = 24;

is NOT same as

Integer i = new Integer(24);

Whatever Carlos has pointed out (special cases etc) holds for the former case, not in the later case.

b) Carlos talks about "==", which is different than what Xiao is talking about - the equals() method.

Coming back to the question posted by Xiao, The class Integer overrides "equals" method so that any two integers with the same numeric value are considered "equal". As a result, see that

Integer i = new Integer(344);
Integer j = new Integer(344);
System.out.println(i.equals(j)); //true.

On the other hand, the class Value doesnot override the equals() method. The equals() method, as defined in the "Object" class returns the same result as "==". Since two different value instances are never "==", hence the equals() method will return false.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great job neelesh you are absolotely right.
[ September 07, 2006: Message edited by: Vaibhav Chauhan ]
 
Xiao Song
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to Neelesh, I have read your reply, and it made me clear.
the following code is the defination of the Object.equals():

It uses "==", if class Value don't override equals(), and "v1.equals(v2)" is just the same as "v1 == v2", the result is "false" without question.

am I right?

thanks all.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting from the Online reference,

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).


As far as the exact "implementation" of equals() method is concerned,I am not sure whether Object class is implemented in java or in a native language like C.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic