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).
Joined: May 30, 2007
when i did System.out.println(i==I);
it returned true why so??
Here i is primitive type and I is an Object of type Integer
so as far as i understood we are not comparing objects hence here it should return false !!
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 -
why i is not automatically converted to an object of Integer class ?
"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
Joined: Jul 28, 2009
Because 'i' is primitive with no methods whatsoever.
However you can box it yourself to get the desired result like this :
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.