This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
great job neelesh you are absolotely right. [ September 07, 2006: Message edited by: Vaibhav Chauhan ]
Joined: Aug 29, 2006
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?
Joined: Jul 20, 2006
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.