Because variable d is a double (primitive type), and not a Double (instance of wrapper class).
You can't call methods on primitive types, therefore d.equals(...) does not work.
The same holds ofcourse for i, which is an int (primitive type) and not an Integer (object).
When you use the == operator on two values, it will check if the two values refer to the exact same object. Note that if you have to different objects that have the same value, == will return false. For example:
When used on variables of a primitive type, == will compare values. For example:
The equals() method is just a normal method, that you can override in your own classes. It's supposed to compare two objects by value - it should return true when the value of the two objects is the same (and false otherwise).
I think the value in 'I' is unboxed and is then compared with the primitive 'i' . Hence true.
I can't think of any other reason, why it should return true.
Why the rules of auto-boxing and auto-unboxing didn't apply to equals method??(code is given in the third post of this topic thread by the starter). When he was saying -
int i=4;
System.out.println(i.equals(4));
why i is not automatically converted to an object of Integer class ?
SCJP 5
"I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times" - Bruce Lee
in the JLS link you provided,only boxing conversions are clarified(no mention of autoboxing). So, i still don't understand that why (Integer)i part of the above mentioned statement you wrote, can not be done automatically as far as J2SE 5.0 or later versions are concerned.
SCJP 5
"I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times" - Bruce Lee
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!