Wednesday, 12 March 2014

Integer.valueOf(1).equals(Long.valueOf(1))

Answer is false, because even though numeric value is same, they are of different types e.g. Integer and Long because Integer.valueOf(1) will return Integer, while Long.valueOf(1) will return Long, Since equals method of Integer class checks for type using intstanceof operator, this will return false :

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

No comments:

Post a Comment

feedback and suggestion.