why it is not equal?

Integer i =new Integer (42);

Long l =new Long (42);

if(i.equals(1))

System.out.println("Equal");

else

System.out.println("NOT Equal");

Why it is Not Equal?can you explain me?

[403 byte] By [Daiesha] at [2007-10-3 8:49:35]
# 1
if(i.equals(1))Is that 1?
mrjavaa at 2007-7-15 3:58:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi ,i is not equal to l bcoz they r belong to diff Wrapper classes.Object is a class in which is having equals method in it and all other wrapper classes overrides it.
java_usera at 2007-7-15 3:58:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

When you do

i.equals(l) - equals method of the Integer class will be called.

and equals method in Integer class is like dis

public boolean equals(Object obj) {

if (obj instanceof Integer) {

//compare the two objects and return the result

}

return false;

}

The two objects will be compared only if both of them are Integer types, other wise it will simply return false.

www.free-java-tutorials.com

Praneeth2002a at 2007-7-15 3:58:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...