Ayesha Farid wrote:Integer io=new Integer("23");
int i=23;
System.out.println(io.equals(i)); // true ...why? ... but other way round i.equals(io) gives error (i cannot be deprecated...no idea whats dat )
1. Because the value of the
Integer object that
io refers to has the value 23.
2. Because you cannot call a method such as
equals() on a primitive type; note that
i is of the primitive type
int.
Ayesha Farid wrote:
System.out.println(5.00f==5L); // long and float are incompatible types still no error ...why?
Because the
long value
5L is automatically converted to
float using a
widening primitive conversion.
Ayesha Farid wrote:
Long lt=53L;
Integer it=53;
int p=53;
System.out.println(lt==p); //true why the error incomparable dun pop-up ...becoz other way round the next statement execute with error...
The variable
lt is
auto-unboxed to a
long; the value of the variable
p is converted from
int to
long by a
widening primitive conversion, and then the values can be compared. I don't know what you mean by "other way round the next statement execute with error". If you write
p==lt instead of
lt==p it still works in the same way.
Ayesha Farid wrote:
System.out.println(lt==it); //error (Incomparable types) why
Because
lt is a
Long object and
it is an
Integer object, and
Long and
Integer are two classes that are unrelated to each other (one cannot be automatically converted to the other).
Ayesha Farid wrote:
System.out.println(lt.equals(it)); //true becoz they refer to different objects...IS this the reason??
No, the result is
false, and not because
lt and
it are different objects, but because they are instances of different classes. If you pass anything other than a
Long object to the
equals() method of class
Long, it will return
false, and so because
it is not a
Long, the result is
false.