wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on equals()" Watch "Doubt on equals()" New topic
Author

Doubt on equals()

Xiao Song
Greenhorn

Joined: Aug 29, 2006
Posts: 17
Hi,all. Why equals() can not treat class Value and Integer the same?


thanks!
[ September 07, 2006: Message edited by: Xiao Song ]

scjping.......
Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
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

Joined: Aug 29, 2006
Posts: 17
Think you Carlos,
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
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

Joined: Aug 16, 2006
Posts: 115
great job neelesh you are absolotely right.
[ September 07, 2006: Message edited by: Vaibhav Chauhan ]
Xiao Song
Greenhorn

Joined: Aug 29, 2006
Posts: 17
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

Joined: Jul 20, 2006
Posts: 107
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.
 
Consider Paul's rocket mass heater.
 
subject: Doubt on equals()